nested-loops

Is There a More Efficient Way to Write Nested While Loops?

The code below shows nested while loops, but it's not the very efficient. Suppose I wanted to extend the code to include 100 nested while loops. Is there a better way to accomplish this task? <?php $count = 1; $num = 1; $options=3; while ( $num <=$options ) { echo "(".$num . ") "; $num1 = 1; $options1=3; whil...

Iterate Multi-Dimensional Array with Nested Foreach Statement

I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so: int[,] = new int[2,3] { {1, 2, 3}, {4, 5, 6} }; What's the best way to iterate through each dimension of the array with a nested foreach statement? ...

php nested for statements?

I'm trying to process a for loop within a for loop, and just a little wary of the syntax... Will this work? Essentially, I want to run code for every 1,000 records while the count is equal to or less than the $count... Will the syntax below work, or is there a better way? for($x = 0; $x <= 700000; $x++) { for($i = 0; $i <= 1000; $...

Reducing permutations of many nested loops

I decided to do an image to explain this better, I just want to check my thinking is ok, and that I can reduce total permutations by 75%: ...

index a list in a Python for loop

I'm making a for loop within a for loop. I'm looping through a list and finding a specific string that contains a regular expression pattern. Once I find the line, I need to search to find the next line of a certain pattern. I need to store both lines to be able to parse out the time for them. I've created a counter to keep track of the...

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...

C#: Nested conditionals vs continue statement

In using ReSharper recently, it is suggesting I reduce nesting in certain places by inverting if conditions and using the continue statements. nested conditionals: foreach(....) { if(SomeCondition) { //do some things if(SomeOtherNestedCondition) { //do some further things } } } ...

Ruby Nested Loop

Hi, I have a nested loop2(check2) in loop1(check1) but it seems that nested loop2(check2) only runs once. Both loops contain the same array. This script is used to check for a duplicate id in check1. check1=["0", "0", "0", "1", "1", "2", "3", "4", "5", "100", "4294967294", "9", "11", "6", "200", "7", "201", "811", "202", "204", "3000",...

Big-O complexity of nested for loops

I'm confused about the complexity of the following (the operation performed inside the inner loop is in constant time): for(int i=0; i<n; i++) for(int j=i; j<n; j++) is this O(n^2) or O(n)? I figure O(n^2). Any ideas? also the following makes me curious: for(int i=0; i<n; i++) for(j=0; j<i; j++) ...

Is a concurrency approach a good idea for speeding a long iteration?

I have an app that does an iteration to create points on a graph over time. While I'm gathering data for each point across the x-axis I also must execute a recursive lookup which effectually means I have a loop inside another loop. This is not scaling too well. I don't see a lot of examples of using a "divide and conquer" solution on it...

How can I get the total of specific array elements?

I have a function that adds shopping cart data to an array of arrays. The array looks like this: Array ( [0] => Array ( [TypeFlag] => S [qty] => 2 [denom] => 50 [certMessage] => [totalPrice] => 100 ) [1] => Array ( [TypeFlag] => S [qty] => 1 [denom] => 25 [certMessage] => [totalPrice] => 25 ) ) What I need to do is get the total p...

PHP: Is it wise to call a method&nested loop in a foreach loop?

Hi I have a method that returns an array of support tickets. Each support ticket can have many notes so I have a method that returns an array of tickets notes with that ticket id. I want to display the notes alongside the ticket which would mean nesting the get notes inside the foreach loop. foreach($tickets as $ticket){ //display...

Parsing nested/related-value strings in functional style.

$tasks->{t1}{cmdline} = "convert -f %FONT% %1%"; $tasks->{t1}{report} = "Launched as %CMD% by %USER%"; $tid = 't1'; foreach my $parm (keys %{$tasks->{$tid}}) { $tasks->{$tid}{$parm}=~s/%FONT%/$tasks->{$tid}{font}/g; # may evaluate if $parm == 'cmdline'; $tasks->{$tid}{$parm}=~s/%CMD%/$tasks->{$tid}{cmdline}/g; } The code is bad...

Optimize two simple nested loops

I have been trying to optimize the two following nested loops: def startbars(query_name, commodity_name): global h_list nc, s, h_list = [], {}, {} query = """ SELECT wbcode, Year, """+query_name+""" FROM innovotable WHERE commodity='"""+commodity_name+"""' and """+query_name+""" != 'NU...

Help with For Loop. Values repeating

$teams = array(1, 2, 3, 4, 5, 6, 7, 8); $game1 = array(2, 4, 6, 8); $game2 = array(); if teams[x] is not in game1 then insert into game2 for($i = 0; $i < count($teams); $i++){ for($j = 0; $j < count($game1); $j++){ if($teams[$i] == $game1[$j]){ break; } else { array_push($game2, $teams[$i]);...

Bash Nested Loops, mixture of dates and numbers

Hi, I am trying to output a range of commands with different dates and numbers associated. for each hour eg. Output im trying to do in a loop is: shell.sh filename<number e.g. between 1-24> <date e.g. 20100928> <number e.g. between 1-24> <id> So basically the the above will generate an output done 24 times for each particular day w...

Unix - Nested Loop. One loop to untar then another to inspect each file in directory

I'm trying to loop round a load of tar files and then move the extracted files into a new folder, inspect them and delete them before moving onto the next tar. Code is below: for i in * do tar -zxvf $i mv *TTF* encoded cd encoded for j in * do echo $j done rm -f *TTF* cd .. done When it gets to the nested loop, ...

golfscript nested while loops

Are nested while loops broken in golfscript or do I not know how to use them? I want to iterate Q from 5 to 0, and for each iteration, iterate Z from 10 to 0. The single loops work well separately, and they seem self-contained (not relying on the stack between operations): 5:Q; {"Q:"Q+ p Q} { Q 1- :Q; }while 10:Z;{"Z:"Z+ p Z}{Z 1- :...

javascript: for (i in str), nested, weird results outside of FF?

I'm rather confused at the moment, could someone explain this one to me? Maybe it's something small I'm oblivious to, but I'm confused as to why this isn't resulting as I expect it to. I have created a samples to show the issue I'm seeing... Sample 1 var dataString = "abc"; document.write(" This is a test ... " + "<br/>") for (i in d...

Find out which combinations of numbers in a set add up to a given total

I've been tasked with helping some accountants solve a common problem they have - given a list of transactions and a total deposit, which transactions are part of the deposit? For example, say I have this list of numbers: 1.00 2.50 3.75 8.00 And I know that my total deposit is 10.50, I can easily see that it's made up of the 8.00 and ...