loops

c# Force loop to check condition

How do I force a loop to check its condition after every line of execution instead of only when the entire block finishes? I have a while(!statement) { } loop, but statement can be changed by several different methods and should force the loop to break immediately after the current line in the loop has finished executing; instead of whe...

C Main Loop without 100% cpu

#include <stdio.h> int main() { while(!DONE) { /* check for stuff */ } return 0; } The above code sample uses 100% cpu until DONE is true. How can I implement a program that loops and only terminates when DONE, but which doesn't use 100% cpu? Modern languages use something like App.ProcessMessages or something like that to g...

C# instantiate in foreach loop?

Possible Duplicate: Loops and Garbage Collection foreach (ObjectTypeA typea in ObjectTypeACollection) { var objectTypeAProcessor= new objectTypeAProcessor(); objectTypeAProcessor.Process(typea); } I found the above similar code where a collection of objects was being processed at the BLL and the DAL processor was called.Will ...

conditional foreach loop c#

How can i do this? (c#) Basically, i want to be able to sort of, comment out a bracket. but not really. I guess I want to close brackets out of order. That's probably not possible. I can obviously implement this in full separate if clauses, but this would considerably lighten my code. P.S.: I am trying to place the "//do something" code...

How can I loop through a list of functions in Perl?

I have a list of functions in Perl. Example: my @funcs = qw (a b c) Now they all belong to this module Foo::Bar::Stix. I would like to call them iteratively in a loop: foreach $func (@funcs) { Foo::Bar::Stix::$func->(%args) } where args is a hash of arguments. However I keep getting this error: "Bad name after :: ..." at the li...

Is it a bad idea to use pointers as loop incrementers instead of the usual "int i"?

An example of this would be: char str[] = "Hello"; int strLength = strlen(str); for ( char * pc = str; pc < str + strLength; pc++) { *pc += 2; } Edit: Accounted for write-protected memory issue. ...

Should "while loops" be preferred to "for loops" for large, necessary loops in R?

Realizing that loops are usually not ideal in R, sometimes they are necessary. When writing large loops, doesn't for (i in 1:large_number) waste memory, since a vector of size large_number must be created? Would this make while loops the best choice for large, necessary loops? ...

Why can't I use foreach on Java Enumeration?

Why can't I do: Enumeration e = ... for (Object o : e) ... ...

while loop to update dates in DB only updates first row?

I have no idea why this won't work. function query($sql) { $this->result = @mysql_query($sql, $this->conn); return($this->result != false); } function convert() { $this->db->open(); $sql_update = ""; $this->db->query("SELECT * FROM ACCOUNTS "); $str = ''; while ($row = $this->db->fetchassoc()...

Uses of 'for' in Java

I am fairly new to Java and in another Stack Overflow question about for loops an answer said that there was two uses of for in Java: for (int i = 0; i < N; i++) { } for (String a : anyIterable) { } I know the first use of for and have used it a lot, but I have never seen the second one. What is it used to do and when would I use it...

C++ delete from vector in for loop crash

Hi, I'm having a problem with my looping over a vector, and deleting values from another vector sometimes crashes my program. I have this vector of int's to keep track of which elements should be removed. std::vector<int> trEn; Then I loop through this vector: struct enemyStruct { float x, y, health, mhealth, speed, turnspeed; ...

R: Using the apply function on a data frame. Help me get my vector, Victor.

I'm trying to normalize some data which I have in a data frame. I want to take each value and run it through the pnorm function along with the mean and standard deviation of the column the value lives in. Using loops, here's how I would write out what I want to do: #example data hist_data<-data.frame(matrix(rnorm(200,mean=5,sd=.5),nrow=...

Loop through labels iPhone SDK

Ok I have 8 labels and I want to loop through them but am having no luck. This is what I have tried. for (int i; i = 0; i < 10; i++) { double va = [varible1.text doubleValue] + i; int j = 0 + I label(j).text= [[NSString alloc]initWithFormat:@"%2.1f", va]; } This errors out. My labels are named like this label0, label1, label...

Nested foreach()

I have the following array: Array ( [1] => Array ( [spubid] => A00319 [sentered_by] => pubs_batchadd.php [sarticle] => Lateral mixing of the waters of the Orinoco, Atabapo [spublication] => Acta Cientifica Venezolana [stags] => acta,confluence,orinoco,rivers,venezuela,waters [authors] => Array ( [1] =...

JS looping and populating array. Which is faster?

I just saw a video of Nicholas Zakas of Yahoo, at GoogleTalks talking about speeding up your website. One of the things he mentioned was doing loops in reverse order to skip one of two comparisons: for (i = len; i--;) {} And he said to keep away from JS libraries implementations of for each. Just for fun I thought I'd try it out. Turns ...

Find Duplicate Values in a dictonary

Hi all, I'm writing a small program to find duplicate files I iterate through each file in a directory then i load the file path and the md5hash of that file into a dictionary ( file path being the key) I next want to walk through each value in the dictionary to see if any values match then display the two+ keys in a display window ...

problem with searching subdirs

I snaged this sub off the web to recursively search all the files including subdirectoires If i point this sub to a large area ( ie mydocuments or C:) I get an error: The CLR has been unable to transition from COM context 0x1f6c48 to COM context 0x1f6db8 for 60 seconds. The thread that owns the destination context/apartment ...

Using jQuery each to grab image height

I have a bunch of images on a page. I'm trying to use jQuery to grab the height of each image and display it after the image. So here is my code: $(document).ready(function() { $(".thumb").each(function() { imageWidth = $(".thumb img").attr("width"); $(this).after(imageWidth); }); }); <div class="thumb"><img src="" borde...

How to modify, without any loop, a collections values to get a new collection?

How do I, without using any loop, modify the values in a collection to get a new collection with the modified values? For example, I'm having a Collection<String> and want to surround all Strings by parentheses. With a loop I would do this: Iterable<String> collection = getCollection(); ArrayList<String> newCollection = new ArrayList<...

Accessing elements with offsets in Python's for .. in loops

I've been mucking around a bit with Python, and I've gathered that it's usually better (or 'pythonic') to use for x in SomeArray: rather than the more C-style for i in range(0, len(SomeArray)): I do see the benefits in this, mainly cleaner code, and the ability to use the nice map() and related functions. However, I am quite often ...