foreach

c++ for loop vs foreach

Which is better( or faster) c++ for loop or the foreach operator provided by Qt? For example,the following condition QList listofstrings; Which is better? foreach(QString str, listofstrings) { //code } or int count = listofstrings.count(); QString str = QString(); for(int i=0;i<count;i++) { str = listofstrings.at(i); //code } ...

Ignorance OR bug on PHP's "foreach" construct?

I have a dataset obtained from MySQL that goes like this: Array ( [0] => Array ( [views] => 14 [timestamp] => 06/04 [views_scaled] => 4.9295774647887 [unix_time] => 1239022177 ) [1] => Array ( [views] => 1 [timestamp] => 19/04 ...

In C++ how can I use a template function as the 3rd parameter in std::for_each?

I am trying to use std::for_each to output the contents of vectors, which may contain different types. So I wrote a generic output function like so: template<typename T> void output(const T& val) { cout << val << endl; } which I would like to use with: std::for_each(vec_out.begin(), vec_out.end(), output); but the compiler comp...

What is the most elegant way to do "foreach x except y" in PHP?

I want to do something like this: foreach ($array as $key=>$value except when $key="id") { // whatever } ... without having to put an "if" clause inside the body of the loop. It is not guaranteed that "id" will the be the first or last element in the array, and I don't really want to unset or slice the array, because that will be expe...

using BOOST_FOREACH with std::map

I'd like to iterate over a std::map using BOOST_FOREACH and edit the values. I can't quite get it. typedef std::pair<int, int> IdSizePair_t; std::map<int,int> mmap; mmap[1] = 1; mmap[2] = 2; mmap[3] = 3; BOOST_FOREACH( IdSizePair_t i, mmap ) i.second++; // mmap should contain {2,3,4} here Of course this doesn't change anything...

How to exit a Generic list ForEach with delegate ?

How do i exit a Generic list ForEach with a delegate? Break or return doesn't work. Example: Peoples.ForEach(delegate(People someone) { if(someone.Name == "foo") ???? What to do to exit immediatly ? }); ...

GridView For Each

I have a gridview that displays items details, I added two template fields one is a checkbox and the other is a textboc, what I want is simply to check all the items the customer wants to buy, and write down the quantity in the textbox, when I click on a button, I should check all rows in the gridview and when the checkbox is Checked the...

C# Foreach Loop - Continue Issue

I have a problem with a continue statement in my C# Foreach loop. I want it to check if there is a blank cell in the datagridview, and if so, then skip printing the value out and carry on to check the next cell. Help appreciated greatly. Here is the code: foreach (DataGridViewRow row in this.dataGridView1.Rows) { ...

C# Foreach Loop Hashtable Issue

I have some code which populates a hashtable with a question as the key and an arraylist of answers as the value. I want to then print out these values from the hashtable so that it displays the question and corresponding solutions for each individual question in the hashtable. I know I have done something totally stupid with the forea...

remove from a List<T> within a foreach

I have code that I want to look like this: List<Type> Os; ... foreach (Type o in Os) if (o.cond) return; // quiting early is important for my case! else Os.Remove(o); ... // other code This doesn't work because you can't remove from a list inside a foreach over that list: Is there a common way to solve th...

My script stops after a foreach loop

Hi, My script is acting strange. After a foreach loop, the script stops. I don't have any error, any warning or notice from Apache. This is the code: foreach($clientFact as $line) { $cliTemp1[] = $line["idcliente"]; echo "qwerty"; } echo "123"; If I add an "echo(qwerty")" inside the loop, it will show the "qwerty" but, right ...

how do you execute a function on a List of objects using LINQ

I want to execute a function on all the objects within a List of objects using LINQ. I know i saw something similar on SO before but after several failed search attempts, i am posting this question ...

XSLT: Foreach iterates for each item, but displays the value of the first item?

Hi, I have a item list and for each item I want to make it an url. List: <root> <tags> <tag>open source</tag> <tag>open</tag> <tag>advertisement</tag> <tag>ad</tag> </tags> </root> XSLT: <xsl:template match="*"> <div class="tags"> <xsl:for-each select="/post/tags/tag"> <a href="#"> ...

How can I make this multidimensional array and these foreach loops work together?

// a beautiful multidimensional array public $form = array ( array( 'field' => 'email', array( 'params' => array( 'rule' => 'email', 'on' => 'create', 'required' => true, ), ), ...

Converting a collection.foreach from c# to VB.Net

In C# i just put the method in the parentheses that i want to run on each row of the collection, but it isn't working in VB.NET. ex: SubSonic.PartCollection Parts; ... Parts.ForEach(TestMethod); I've tried this in VB.Net, but it's not compiling, and i'm not quite sure what I'm missing. Dim Parts as SubSonic.PartCollection ... parts....

Call member functions of members of elements of a container with for_each?

Confusing title, hopefully some code will clarify: struct MyNestedType { void func(); }; struct MyType { MyNestedType* nested; } std::vector<MyType> vec; // ... populate vec // I want something approximating this line, but that doesn't use made-up C++! std::for_each(vec.begin(), vec.end(), std::mem_fun_ref(&MyType::nested->f...

Lambda Expression using Foreach Clause...

EDIT For reference, here's the blog post which eric referrrred to in the comments http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx ORIG More of a curiosity I suppose but one for the C# Specification Savants... Why is it that the ForEach() clause doesn't work (or isn't available) for use on IQueryable/IEnu...

editing a multidimensional array with [index]es, not only [name]s

public $form = array ( array( 'field' => 'email', 'params' => array( array( 'rule' => 'email', 'on' => 'create', 'required' => true, 'error' => 'The email is invalid!' ), array( 'ru...

Is there a way to do a foreach on each element pair in 2 IEnumerable's in C#?

I could convert them to lists and just use a regular for loop with indexes, but I'm wondering if there's a way to do it that keeps them as IEnumerables. ...

Action(Of T) in Visual Basic in List(Of T).ForEach

I have searched high and low for documentation on how to use this feature. While the loop I could write would be simple and take no time, I really would like to learn how to use this. Basically I have a class, say, Widget, with a Save() sub that returns nothing. So: Dim w as New Widget() w.Save() basically saves the widget. Now let'...