iteration

For each string, execute a function/procedure

I'd like to loop through a list of strings and execute a function/procedure with each string as the argument. What's the best alternative to the following generic code (since it's not legal): set serveroutput on; begin FOR r IN ('The', 'Quick', 'brown', 'fox') LOOP dbms_output.put_line( r ); END LOOP; end; I assu...

How can I re-use WHERE clause logic with DBI?

Hi all: Disclaimer: first time I've used DBI. I have a MySQL table with a lot of indexed fields (f1, f2, f3, etc) that are used to generate WHERE clauses by long-running processes that iterate over chunks of the database performing various cleaning and testing operations. The current version of this code works something like this: s...

PHP get last iteration foreach loop value

Ok I have a foreach loop and wanted to know if I could get the last iteration data value for the current iteration? Code: foreach($array as $key=>$data) { echo "Key: ".$key." Data: ".$data."<br />"; } Results: Key: 0 Data: 23244 Key: 0 Data: Program ID: 39-1-1499-1 Results I would like: Key: 23244 Data: Program ID: 39-1-1499-1...

How to iterate by row through a mysql query in php

Ok, So I am trying to query my database and select all rows that have a certain value. After that I turn the query into an array with mysql_fetch_array(), then I tried iterating by row through the fetched array using a for each loop. <?php $query = mysql_query("SELECT * FROM users WHERE pointsAvailable > 0 ORDER BY pointsAvailable Desc"...

Iterative version of a recursive algorithm is SLOWER!

I'm trying to implement an iterative version of Tarjan's strongly connected components (SCCs), reproduced here for your convenience (source: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm). Input: Graph G = (V, E) index = 0 // DFS node number counter S = empty ...

Python: Fastest way to iterate this through a large file

Right, I'm iterating through a large binary file I need to minimise the time of this loop: def NB2(self, ID_LEN): r1=np.fromfile(ReadFile.fid,dTypes.NB_HDR,1) num_receivers=r1[0][0] num_channels=r1[0][1] num_samples=r1[0][5] blockReturn = np.zeros((num_samples,num_receivers,num_channels)) for rec in range(0,nu...

$.each([collection]) vs $([collection]).each()

Both methods appear to produce the same results, but I've been hard-pressed to actually convince people that the second method works, since it's apparently not commonly known. // Create some data var foo = { 'vals':[ {'id':'foo'}, {'id':'bar'} ] }​​​​​​​​​​​​​​​​​; // Common Method $.each(foo.vals, function(i,o){ alert(this.id)...

I'm trying to change my Maze traversal recursive coding part into a while loop.

Here's my code. #include <iostream> using namespace std; enum Direction { EAST, NORTH, WEST, SOUTH }; const int size = 12; int xStart = 2; int yStart = 0; char *maze2[ ] = { "############", "#...#......#", "..#.#.####.#", "###.#....#.#", "#....###.#..", "####.#.#.#.#", "#..#.#.#.#.#", "##.#.#.#.#.#", ...

Iterating through a variable length Java array

How do I iterate over a Java array of variable length. I guess I would setup a while loop, but how would I detect that I have reached the end of the array. I guess I want something like this [just need to figure out how to represent myArray.notEndofArray()] index = 0; while(myArray.notEndofArray()){ system.out.println(myArray(index...

How to iterate through an array starting from the last element? (Ruby)

I came with below solution but I believe that must be nicer one out there ... array = [ 'first','middle','last'] index = array.length array.length.times { index -= 1; puts array[index]} ...

Setting Hashmap in a loop

I have the following code: Map<String, ObjectType> objectMap = new HashMap<String, ObjectType>(); for (ObjectType obj : objects) { obj.setSomeProperty("property value"); objectMap.put(obj.getADiffProperty(), obj); } It seems like during loop iteration some of the obj property changes for different keys than the one currently b...

Iterative Models and Stateless Environments

I have an iterative simulation model that does X per timestep. What's the best way to simulate that iterative model in a stateless environment like ASP.NET? Simulation data is in the 10 to 15 MB range. ...

PHP foreach over an array of objects

I'm trying to use a foreach loop for an array of objects. Inside of the BeginBattle() method I want to iterate through all of the objects and increment their played count automatically. Unfortunately, the web browser shows I have an error: Fatal error: Call to a member function BattleInitiated() on a non-object in /nfs/c05/h01/mnt/7029...

Efficient looping through AS3 dictionary

for (var k in dictionary) { var key:KeyType = KeyType(k); var value:ValType = ValType(dictionary[k]); // <-- lookup // do stuff } This is what I use to loop through the entries in a dictionary. As you can see in every iteration I perform a lookup in the dictionary. Is there a more efficient way of iterating the dictionary (while...

Is iteration necessary in the following piece of code?

Here's a piece of code from the xss_clean method of the Input_Core class of the Kohana framework: do { // Remove really unwanted tags $old_data = $data; $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data); } while ($ol...

Ruby: How to 'next' an external loop?

file.each_line do |line| #skip the first one/not a user 3.times { next } if first == 1 first = 2 end How can I get the 'next' to well, "next" the iteration of the each_line, instead of the 3.times iteration? Also, how can I write this to look better (ie: first == 1 looks bad) ...

How can I compute a Cartesian product iteratively?

This question asks how to compute the Cartesian product of a given number of vectors. Since the number of vectors is known in advance and rather small, the solution is easily obtained with nested for loops. Now suppose that you are given, in your language of choice, a vector of vectors (or list of lists, or set of sets, etc.): l = [ [1...

idiomatic way to take groups of n items from a list in Python?

Given a list A = [1 2 3 4 5 6] Is there any idiomatic (Pythonic) way to iterate over it as though it were B = [(1, 2) (3, 4) (5, 6)] other than indexing? That feels like a holdover from C: for a1,a2 in [ (A[i], A[i+1]) for i in range(0, len(A), 2) ]: I can't help but feel there should be some clever hack using itertools or slici...

How to execute T-SQL for several databases whose names are stored in a table

Hey guys, so here's the deal. I have several databases (SqlServer 2005) on the same server with the same schema but different data. I have one extra database which has one table storing the names of the mentioned databases. So what I need to do is to iterate over those databases name and actually "switch" to each one (use [dbname]) an...

Cannot iterate of a collection of Anonymous Types created from a LINQ Query in VB.NET

Ok everyone, I must be missing something here. Every LINQ example I have seen for VB.NET anonymous types claims I can do something like this: Dim Info As EnumerableRowCollection = pDataSet.Tables(0).AsEnumerable Dim Infos = From a In Info _ Select New With {.Prop1 = a("P...