loops

Help modifying recursive function

Given a canvas, let's say 10x10, and given 3 rectangles/squares. Canvas = 10x10 Rectangle 1 = 2x2 Rectangle 2 = 3x3 Rectangle 3 = 2x4 I've created a recursive function that loops every position of every rectangle on the canvas, and it works fine. (I've included the function below incase anyone wants to see it but I don't think it's n...

Looping in function or multiple calling the function, what is faster?

Hi, basicly I've got an array and want to call the same function for each element. Which way is it faster? foreach($elemeents as $element){ callFunction($element); } OR function callFunction($leements){ foreach($elements as $element){ //do something } } thanx in advance, im just a beginner ...

Can I use a loop to execute the same function call multiple times on incrementing element Id's?

Right now I have 2 functions called showElement (elementId) and hideElement (elementId). I use these to hide and display rows in a table based on what a user clicks. I have a submit button for an area that takes in about 30 rows and looks like the following function hideGeneralSection { hideElement('gen1'); hideElement('gen2'); hideElem...

Why doesn't this for loop process the full data set?

Background I have a spreadsheet of ticket allocations for an event. On each row of the spreadsheet is a name and the number of tickets allocated. I need to change the spreadsheet so that each name is duplicated once per ticket on separate rows, like this: I have a macro to do this, however it exhibits strange behaviour The Proble...

Use a variable within a variable? Java

I have several fields, each one is like this: field1 field2 field3 ... Using a loop with a counter, I want to be able to say fieldx. Where x is the value of the counter in that loop. This means if I have 6 entries in my array, fields1 - field6 will be given values. Is fieldx possible? ...

Wordpress - query_posts

Is there a way to query multiple specific posts? For example, the following gets one: // retrieve one post with an ID of 670 query_posts('p=670'); But as far as I can tell, I can't extend this to do two posts, separated by commas. Does anyone know a better way? ...

gdb & finding out when a memory address is written to

I am trying to figure out when a certain memory address is written to. I have tried couple of different loops in gdb but it never stoped. Any ideas? Ex: (gdb) while *0x68181b88 == 0 > step > end PS: This is a mips linux system. Edit: My MIPS does not have hw support/registers to watch memory values. Although watch works, it takes ab...

For Loop repeats first loop twice

I have this for loop, and it seems to be repeating the first loop twice (x=0) and then not doing the last one (x=2) for (x=0;x<=2;x++) { if (document.getElementById("sub"+catCount+x).value != "") { if (nonums.test(document.getElementById("sub"+catCount+x).value)) { total = tota...

Problem with "while" in Java

I'm trying out several exercises from a Java programming book. I have the code below: import java.io.*; import java.util.Scanner; public class Ex420 { public static void main( String args[] ) { String employeeName = ""; double workHours,excessHours, hourlyRates, grossPay; Scanner input = new Scanner( System.in ); while ( emp...

Perl: Multiple global "or"-separated regex conditions in while block leads to an infinite loop?

Hi all, I'm learning Perl and noticed a rather peculiar quirk -- attempting to match one of multiple regex conditions in a while loop results in that loop going on for infinity: #!/usr/bin/perl my $hivar = "this or that"; while ($hivar =~ m/this/ig || $hivar =~ m/that/ig) { print "$&\n"; } The output of this program is: th...

How to lessen this comparing loop

I need to find which id numbers are missing inside s.data compared to users. Is there a better(smaller code) way to compare? Thanks ;) if(users.length != undefined) { for(y=0;y<users.length;y++) { var left = true; for(y2=0;y2<s.data.length;y2++) { if(users[y].client_id==s.data[y2].client_i...

Loops inefficiency in R

Good morning, I have been developing for a few months in R and I have to make sure that the execution time of my code is not too long because I analyze big datasets. Hence, I have been trying to use as much vectorized functions as possible. However, I am still wondering something. What is costly in R is not the loop itself right? I m...

can you prevent a php while loop from erroring out?

I was wondering if there was a way to prevent a while loop from prematurely erroring out or terminating. I've thrown a try/catch in there and it seems to keep terminating. (As to the cause why it's terminating, I'm still debugging). $stomp = $this->stomp; if(isset($queue) && strlen($queue) > 0) { error_log('Starting Moni...

Python: If an iterator is an expression, is it calculated every time?

Take the following example: >>> for item in [i * 2 for i in range(1, 10)]: print item 2 4 6 8 10 12 14 16 18 Is [i * 2 for i in range(1, 10)] computed every time through the loop, or just once and stored? (Also, what is the proper name for that part of the expression?) One reason I would want to do this is that I only want the ...

How do I loop through a mysql query with php

I tried to use this function $conn = db_connect(); while ($newsfeed = $conn->query("select info, username, time from newsfeed ORDER BY time DESC LIMIT 10")) { (...) echo "<p>User $newsfeed_username just registerted ".$minutes." min ago </p><br>"; but it only shows the la...

Looping through localStorage in HTML5 and JavaScript

So, I was thinking I could just loop through localStorage like a normal object as it has a length. How can I loop through this? localStorage.setItem(1,'Lorem'); localStorage.setItem(2,'Ipsum'); localStorage.setItem(3,'Dolor'); If I do a localStorage.length it returns 3 which is correct. So I'd assume a for...in loop would work. I was...

WordPress loop by function

Hello, I'm creating magazine style theme (not e-commerce) and I want to display 3 latest posts from ex. X, Y and Z category, where this 1st post will be with thumb and other 2 only titles. I found some similar solution themes, but when I look into the code, they created 2 loop for each category (2x3=6) and with this 6 loops code looks ve...

Using MATLAB loop funtion to name calculate variables

I need to calculate the mean, std, and other numbers for my programming and I was wondering how to use the loop function to my advantage. I have 5 electrodes of data. So to calculate the mean of each I used: mean_ch1 = mean(ch1); mean_ch2 = mean(ch2); mean_ch3 = mean(ch3); mean_ch4 = mean(ch4); mean_ch5 = mean(ch...

Strange things in JavaScript "for"

Hi. I'm using jQuery and I have a strange thing that I don't understand. I have some code: for (i = 1; i <= some_number; i++) { $("#some_button" + i).click(function() { alert(i); }); } "#some_button" as the name says - they are some buttons. When clicked they should pop-up a box with it's number, correct? But they don'...

How to update a Ruby hash item inside of a loop over that hash?

I am migrating my PHP code to Ruby and at some point I need to update hash elements inside of a loop. For example: compositions.each_pair do |element,params| params['composition'].each_pair do |item,data| data['af'] /= params['af sum'] data['mf'] /= params['mass'] end end I could make it using item indexes, but it will be...