for-loop

Alternative to 'for i in xrange(len(x))'

So I see in another post the following "bad" snippet, but the only alternatives I have seen involve patching Python. for i in xrange(len(something)): workwith = something[i] # do things with workwith... What do I do to avoid this "antipattern"? ...

VB.NET Infinite For Loop

Is it possible to write an infinite for loop in VB.NET? If so, what is the syntax? ...

msbuild exec task with for

I am trying to run the following commands as part of a msbuild script: for /R . %f in (*.targets) do copy /Y "%f" "C:\Program Files (x86)\MSBuild\Microso ft\VisualStudio\TeamBuild" The commands is implemented in an exec the following way: <Exec WorkingDirectory="$(SolutionRoot)" Command="for /R . %f in (*.targets) do copy /Y &quot;%f...

What's the difference between iterating over a file with foreach or while in Perl?

I have a filehandle FILE in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following? while (<FILE>) { # do something } and foreach (<FILE>) { # do something } ...

a question on for loops in python

hi, I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks. import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(10000, 1000): if x*x == y*y + ...

Which is better practice - for loop with break or conditional loop?

I'm just curious what peoples' thoughts are on this topic. Let's say I have an Array of Objects, and I want to loop through them to see if the Objects contain certain values, and if so, I want to stop the loop. Which is better practice - a for loop with a break, or a conditional loop? The pseudo-code in the example I have provided is fo...

Using for...else in Python generator expressions

I'm a big fan of Python's for...else syntax - it's surprising how often it's applicable, and how effectively it can simplify code. However, I've not figured out a nice way to use it in a generator expression, for example: def iterate(i): for value in i: yield value else: print 'i is empty' In the above example...

How to make this Groovy string search code more efficient?

I'm using the following groovy code to search a file for a string, an account number. The file I'm reading is about 30MB and contains 80,000-120,000 lines. Is there a more efficient way to find a record in a file that contains the given AcctNum? I'm a novice, so I don't know which area to investigate, the toList() or the for-loop. th...

for loop in objective-c - primitive array

int matrix[3][3] = { {1,2,3}, {1,2,3}, {1,2,3}, } how can i loop over it? basically the length operation is my concern. for (int i=0; XXXXX; i++) { for (int j=0; XXXX; j++) { int value = matrix[i][j]; } } EDIT: is there a dynamic way of getting the array size? something like sizeof() thank you, chris ...

What is the special case with the foreach loop that eliminates bounds checking?

What is the special case with the foreach/for loop that eliminates bounds checking? Also which bounds checking is it? ...

Does Pre and post increment/decrement operators in C++ have same performance in a loop?

Consider following two examples. class ClassOne { //class definition is here }; std::vector< ClassOne > myListOfObjects; std::vector< ClassOne >::const_iterator iter = myListOfObjects.begin(); Example 1: for( ; iter < myListOfObjects.end(); **++iter**) { //some operations } OR Example 2: for( ; iter < myListOfObjects.end(); *...

What's the cleanest way to walk and unwalk a std::vector using iterators?

I have a situation where I'm marching through a vector, doing things: std::vector::iterator iter = my_list.begin(); for ( ; iter != my_list.end(); ++iter ) { if ( iter->doStuff() ) // returns true if successful, false o/w { // Keep going... } else { for ( ; iter != m_list.begin(); --iter ) // ...This won't work......

for loop in Flex 3

i want to create a loop withing my mxml code to create a variable number of input fields based on an integer value , this value is the result of a call which gets the number of columns in a database table. i have tried to use the repeater component however it needs an array, and my call is an int. Is there something in flex mxml whcih ...

Simple for loop question.

Currently I am studying for my Java test. Whist studying I've come across a small problem. In this for loop: for ( int i=1; i <= 3 ; i++ ) { for (int j=1; j <= 3 ; j++ ) { System.out.println( i + " " + j ); } } The output is: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 My problem is, I don't understand it. When I read thi...

What's the best way to do a reverse 'for' loop with an unsigned index?

My first attempt of reverse for loop that does something n times was something like: for ( unsigned int i = n-1; i >= 0; i-- ) { ... } This fails because in unsigned arithmetic i is guaranteed to be always greater or equal than zero, hence the loop condition will always be true. Fortunately, gcc compiler warned me about a 'po...

Looping through and combining two files in UNIX

This should be simple for those of you who have some programming knowledge... Unfortunately I don't. I'm trying to iterate through a text file of image captions and add them as title tags to an html file. The image captions file has 105 captions (each is separated by a carriage return) and the gallery file has blank alt tags on each a t...

Creating multiple TextInput fields in for loop

HI, I need to loop through an array and for each element create a textfield. My problem is how to create a new identifier for each new TextInput this is my code; var count:Number = 0; for (var i:String in columnsData) { var myTI:TextInput = new TextInput(); myTI.width = 70; myTI.height = 25; myTI.text = columnsData[i]; myTI.name = "my...

Pass strings to function in a loop

I have this code that activates when rollover, rollout, and release. I functions for rollover and rollout works, but the release function does not. I'm trying to pass some strings with url's to the function within a loop. var url1:String = "http://www.google.com"; var url2:String = "http://www.google.com"; var url3:String = "http://www....

Which version of python added the else clause for for loops?

Which was the first version of python to include the else clause for for loops? I find that the python docs usually does a good job of documenting when features were added, but I can't seem to find the info on this feature. (It doesn't help that 'for' and 'else' are particularly difficult terms to google for on a programming website) ...

Pythonic ways to use 'else' in a for loop

I have hardly ever noticed a python program that uses else in a for loop. I recently used it to perform an action based on the loop variable condition while exiting; as it is in the scope. What is the pythonic way to use an else in a for loop? Are there any notable use cases? And, yea. I dislike using break statement. I'd rather set t...