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? ...
Almost all languages have a foreach loop (function) or something similar. I wonder if C has one? Can you post some example code? ...
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: "...
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...
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...
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...
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...
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 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...
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; } ...
Is there a C# equivalent of Python's enumerate() and Ruby's each_with_index? ...
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 ...
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...
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...
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...
$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:...
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? ...
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 ...
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...
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...