nested-loops

Breaking out of a nested loop

If I have a for loop which is nested within another, how can I efficiently come out of both loops (inner and outer) in the quickest possible way? I don't want to have to use a boolean and then have to say go to another method, but rather just to execute the first line of code after the outer loop. What is a quick and nice way of going ...

What is the Big-O of a nested loop, where number of iterations in the inner loop is determined by the current iteration of the outer loop?

What is the Big-O time complexity of the following nested loops: for(int i = 0; i < N; i ++) { for(int j = i + 1; j < N; j++) { System.out.println("i = " + i + " j = " + j); } } Would it be O(n^2) still? ...

Is it possible to exit a for before time in C++, if an ending condition is reached?

Hello, I want to know if it is possible to end a for loop in C++ when an ending condition (different from the reacheing right number of iterations) is verified. For instance: for (int i = 0; i < maxi; ++i) for (int j = 0; j < maxj; ++j) // But if i == 4 < maxi AND j == 3 < maxj, // then jump out of the two nested lo...

Replace Nested For Loops... or not

I have a script that loops through a series of four (or less) characters strings. For example: aaaa aaab aaac aaad If have been able to implement it with nested for loops like so: chars = string.digits + string.uppercase + string.lowercase for a in chars: print '%s' % a for b in chars: print '%s%s' % (a, b) ...

PHP nested loop behaving unexpectedly

I have an array which contains the categories for a particular article ($link_cat). I'm then using mysql_fetch_array to print out all of the categories available into a list with checkboxes. While it's doing this I want it to compare the value it's on, to a value from the other array. If there is a match, then it means that one of the ca...

What's a good way to structure variable nested loops?

Suppose you're working in a language with variable length arrays (e.g. with A[i] for all i in 1..A.length) and have to write a routine that takes n (n : 1..8) variable length arrays of items in a variable length array of length n, and needs to call a procedure with every possible length n array of items where the first is chosen from the...

How to iterate over joined data in two or more nested loops?

Some time ago I asked a question about nested loops on SO and as it was, there were queries inside the loops of my example and I got a clear answer: NEVER EVER NEVER put an SQL query inside a loop I've tried ever since and mostly it works. Just need to make an effort and write a query that retrieves all you need at once. BUT what ...

Access db loop - for each record in one table create array of records in another table

Is it possible to create a nested looping query in Access DB that will update a third table? I have a master (header) table: ------------------------ masters ------------------------ num | modality | cost | ------------------------ 01 | thing | 23.00 | 02 | thing | 42.00 | 03 | thing | 56.00 | 04 | apple | 11.00 | 05 ...

C pointer arithmetic snippet

I have a program that I'm trying to decode. It is translated to C from another language (whose name is not spoken here), and as I want to understand how it works, I am slowly rewriting the code and simplifying it to use all the nice logical constructs C has to offer. The following little bit keeps popping up in my code, with varying val...

Looping Through Set Number of Posts in Wordpress, Then running same loop again on next set, etc.

I have looked and looked and tried to find an answer for what I've been looking for, but I have yet to see an answer for this: I am trying to generate a Wordpress loop that takes all the posts from a single category and displays them three at a time inside <li></li> tags. Output should look like this: <li>My post title | Another Title...

is there a difference in the runtime of the following:

Is there a difference in the runtime of the following two snippets? SNIPPET 1: for ( Object obj : collection ) { step1( obj ); step2( obj ); step3( obj ); } SNIPPET 2: for ( Object obj : collection ) { step1( obj ); } for ( Object obj : collection ) { step2( obj ); } for ( Object obj : collection ) { step3...

combinations: avoiding multiple nested foreach

When you need to check/have combinations of array elements, how can you avoid nesting foreach? Example code: $as = array($optionA1, $optionA2) $bs = array($optionB1, $optionB2) $cs = array($optionC1, $optionC2) foreach ($as as $a) { foreach ($bs as $b) { foreach ($cs as $c) { $result = $this->method($a, $b, $c); ...

Sum the total value of a particular field in a mysql table using php and pulling the whole row in array form

Well the title sucks. But here is what I am trying to do. I have a foreach loop that is already pulling all the rows from a mysql db table using a db object style query in a function. That function is grabbing all fields and setting it up in an array ex foreach ($blahs as $blah => $blah_data) That now allows me to populate the page w...

In python is there an easier way to write 6 nested for loops?

This problem has been getting at me for a while now. Is there an easier way to write nested for loops in python? For example if my code went something like this: for y in range(3): for x in range(3): do_something() for y1 in range(3): for x1 in range(3): do_something_else() would there be an easier ...

How can I handle nested looping without a chain a foreachs in Perl?

Hi, I am new to Perl. I am writting a Perl script to download the same type of data from different sites. I have written the below code: #! /usr/bin/perl use strict; use warnings; my @sites = ('a', 'b', 'c'); my @servers = ('A', 'B'); my @data_type = ("X", "Y", "Z"); foreach my $site (@sites) { foreach my $server_type (@serv...

F# - how to write nested loops in a recursive way?

Given the following C# code: var product = new List<int>(); for (int n1 = 100; n1 < 1000; n1++) { for (int n2 = 100; n2 < 1000; n2++) { product.Add(n1 * n2); } } What would be the equivalent F# code written in a functional style? ...

How can I create combinations of several lists without hardcoding loops?

I have data that looks like this: my @homopol = ( ["T","C","CC","G"], # part1 ["T","TT","C","G","A"], #part2 ["C","CCC","G"], #part3 ...upto part K=~50 ); my @prob = ([1.00,0.63,0.002,1.00,0.83], [0.72,0.03,1.00, 0.85,1.00], ...

Fastest nested loops over a single list (with elements remove or not)

Hello, I am looking for advice about how to parse a single list, using two nested loops, in the fastest way, avoiding doing len(list)^2 comparisons, and avoiding duplicate files in groups. More precisely: I have a list of 'file' objects, that each has a timestamp. I want to group the files by their timestamp and a time offset. Ex. star...

Can anyone see what is wrong with my Javascript?

I have written the following: var pages=["[www.google.co.uk] This is the WWW. ","[www.yahoo.co.uk] This is also the WWW. "]; function findScoresC(s){ var scores=[]; var words=[]; var wordScore; var indexScore=[]; s=s.toLowerCase(); for(i=0;i<pages.length; i++){ var lowerCaseContents=(pages[i].substring(pages[i].indexOf("]")+1,pages[...

Table within a table, table outputted except last <td> in each row except for the first row?

I've got an inventory table that needs to have certain in each row, and then in the last column in each row, i want to loop out each item that was used per a specific inventory update i.e. one row would have one column for the customername, one column for the date of the inventory transaction, one for the type of transaction, the specif...