foreach

C Foreach or Similar

Almost all languages have a foreach loop (function) or something similar. I wonder if C has one? Can you post some example code? ...

Why can't I .add a Dictionary(Key, Value) in a foreach?

If I want to loop over a Dictionary(Key, Value)... why cant I add a new key-value pair in the loop? Dictionary<string, string> persons = new Dictionary<string, string>(); persons.Add("roger", "12"); persons.Add("mary", "13"); foreach (KeyValuePair<string,string> person in persons) { Console.WriteLine("Name: " + person.Key + ", Age: "...

Foreach Loop Microsoft SSIS - equivalent to break statement

Within a Foreach loop in SSIS is there a way that if a task fails you can break out of the loop to the next iteration? I am looping over xml files and performing a lookup using values within this file, if the lookup doesn't return any values i'd like to report on this and then not perform any other tasks for this file. If there is no e...

How do I use a foreach loop in Java to loop through the values in a HashMap?

I am trying to compile the following code: private String dataToString(){ Map data = (HashMap<MyClass.Key, String>) getData(); String toString = ""; for( MyClass.Key key: data.keySet() ){ toString += key.toString() + ": " + data.get( key ); return toString; } I get an error in the for line that says: incompati...

How do I append a newline character for all lines except the last one?

I'm iterating through a HashMap (see my earlier question for more detail) and building a string consisting of the data contained in the Map. For each item, I will have a new line, but for the very last item, I don't want the new line. How can I achieve this? I was thinking I could so some kind of check to see if the entry is the last o...

Iterating over vector and calling functions

I have a class that has a vector of another class objects as a member. In many functions of this class I have to do same operation on all the objects in the vector: class Small { public: void foo(); void bar(int x); // and many more functions }; class Big { public: void foo() { for (size_t i = 0; i < V...

Using std::for_each on polymorphic method in c++

When using the std::for_each, class A; vector<A*> VectorOfAPointers; std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo)); If we have classes inheriting from A and implementing foo(), and we hold a vector of pointers to A, is there any way to call a polymorphic call on foo(), rather then explicitl...

Is there a way to access an iteration-counter in Java's for-each loop?

Is there a way in Java's for-each loop for(String s : stringArray) { doSomethingWith(s); } to find out how often the loop has already been processed? Aside from using using the old and well-known for(int i=0;i<boundary;i++)-loop, is the construct int i = 0; for(String s : stringArray) { doSomethingWith(s); i++; } the only wa...

how to fetch just 1 row from an object

this query will just one row as a result myDataContext db = new myDataContext(); var query = from u in db.users where u.userId == myUserId select u; I usually get the result out from the "query" object by using "foreach" foreach(var i in query){ username = i.username; } ...

C# foreach with index

Is there a C# equivalent of Python's enumerate() and Ruby's each_with_index? ...

Parallel iteration in C#?

Is there a way to do foreach style iteration over parallel enumerables in C#? For subscriptable lists, I know one could use a regular for loop iterating an int over the index range, but I really prefer foreach to for for a number of reasons. Bonus points if it works in C# 2.0 ...

How to break multiple foreach loop?

I have four foreach loops that iterate through the collections and based on a condition do something. Here is the code that I am writing now: boolean breakFlag = false; String valueFromObj2 = null; String valueFromObj4 = null; for(Object1 object1: objects){ for(Object2 object2: object1){ //I get some value from object2 valueFr...

Scrape and generate RSS feed

I use Simple HTML DOM to scrape a page for the latest news, and then generate an RSS feed using this PHP class. This what I have now: <?php // This is a minimum example of using the class include("FeedWriter.php"); include('simple_html_dom.php'); $html = file_get_html('http://www.website.com'); foreach($html->find('td[width="380...

c# using Array.ForEach Action Predicate with array of value type or string

Hi, Am I correct in thinking the following snippet does not work (the array items are not modified) because the array is of integer which is value type. class Program { public static void Main() { int[] ints = new int[] { 1,2 }; Array.ForEach(ints, new Action<int>(AddTen)); // ints is not modified...

unexpected T_ELSEIF

$pages = array("grac", "zamknij", "dolaczyc"); $pagesid = array("showNews", "showThread", "showProfile"); foreach ($pagesid as $page) { if (isset($_GET[$page])) { include('sobra/'.$page.'.php'); } } // just pages elseif (in_array($_GET['page'], $pages)) { include("$_GET[page].php"); } // error else include('error.php'); gives:...

Concise foreach expression in one line

In Perl, one can often avoid using control blocks, like this: print "$_\n" foreach(@files); instead of: foreach(@files){ print "$_\n"; } How does this syntax work in the following, more complex case: die("Not a file: $_") unless -f $_ foreach(@files); It gives me a syntax error. I'm not trying to write obfuscated code, it's ju...

What is the easiest way to iterate over all the key/value pairs of a java.util.Map in Java 5 and higher?

What is the easiest way to iterate over all the key/value pairs of a java.util.Map in Java 5 and higher? ...

Understanding for each loop in Java

The code below doesn't do what I expect. Every string is null after this code executes. String[] currentState = new String[answer.length()]; for(String x : currentState) { x = "_"; } The code below does what I expect. Every string in currentState is now "_" String[] currentState = new String[answer.length()]; for (int i = 0; i ...

Is the .NET foreach statement guaranteed to iterate a collection in the same order in which it was built?

A coworker used a for loop to iterate a List in some C# code he wrote and left the comment, "did't use For Each because I wasn't sure it iterates in order. Who knows what Microsoft will do." For example, suppose we have a List built up like this: var someList = new List<string>(); someList.Add("one"); someList.Add("two"); someList.Ad...

For-Each Loop AS3: Is the direction guaranteed?

I would like to know the iteration order for the Array, Dictionary and Object types in AS3, for both the for-each and the for-in loops. Also what factors can change the iteration order of these loop type combinations? For example I presume that using a for-each on an Array type always move from the first element to the last. For-each ca...