foreach

When removing inside foreach, do we need to step back

Take this Java code: for (ContactsReduced curContact : allcontacts) { .......... allcontacts.remove(curContact); } I do based on a condition a remove, will the foreach miss the next item in order, and do we need to step back(somehow)? ...

In Python, is it better to use list comprehensions or for-each loops?

Which of the following is better to use and why? Method 1: for k, v in os.environ.items(): print "%s=%s" % (k, v) Method 2: print "\n".join(["%s=%s" % (k, v) for k,v in os.environ.items()]) I tend to lead towards the first as more understandable, but that might just be because I'm new to Python and list comprehensions a...

How to Loop through LINQ results (VB.NET)

I've got some code to try and loop through LINQ results, but it doesn't seem to be working. HERE'S THE CODE Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest ''# the page contenttype is plain text' HttpContext.Current.Response.ContentType = "text/pla...

Storing multiple inputs with the same name in a CodeIgniter session

I've posted this in the CodeIgniter forum and exhausted the forum search engine as well, so apologies if cross-posting is frowned upon. Essentially, I've got a single input, set up as <input type="text" name="goal">. At a user's request, they may add another goal, which throws a duplicate to the DOM. What I need to do is grab these valu...

Type casting problem with java for-each loop

Hi, I have traced an issue with an application I am developing, it is giving me a type cast exception. Funny thing is it is saying it cannot cast "entities.Movie cannot be cast to entities.Movie"?! movies is an ArrayList. try { movies = getMovies(); } catch (Exception e) { e.printStackTrace(System.out); } fi...

Any big difference between using contains or loop through a list?

Hi, Performance wise, is there really a big difference between using: ArrayList.contains(o) vs foreach|iterator LinkedList.contains(o) vs foreach|iterator Of course, for the foreach|iterator loops, I'll have to explicitly compare the methods and return true or false accordingly. The object I'm comparing is an object where equals() ...

How do i parse a map (foreach) in the same order i created it (JAVA)

So i have a map that i created (inserted data) in an order i wanted. When parsing the map the 1st key returned in foreach is not the first key i inserted. Is there a way for that to happen? Also sorting my map is kinda tricky cause it has to be sorted by Value and in specific field within the Value. Ty ...

MonoTouch - foreach vs for loops (performance)

Normally I'm well aware that a consideration like this is premature optimization. Right now I have some event handlers being attached inside a foreach loop. I am wondering if this style might be prone to leaks or inefficient memory use due to closures being created. Is there any validity to this thinking? ...

PHP modifying and combining array

Hi everyone, I have a bit of an array headache going on. The function does what I want, but since I am not yet to well acquainted with PHP:s array/looping functions, so thereby my question is if there's any part of this function that could be improved from a performance-wise perspective? $var = myFunction ( array('key1', 'key2', 'key3'...

Iterate Multi-Dimensional Array with Nested Foreach Statement

I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so: int[,] = new int[2,3] { {1, 2, 3}, {4, 5, 6} }; What's the best way to iterate through each dimension of the array with a nested foreach statement? ...

what is the difference between these two foreach loops ?

I have seen in cakephp that foreach loop is used like this foreach($tags as $tag) : \\code here endforeach; and I used to write this style of foreach foreach($tags as $tag) { //code here } what is the difference between these two foeach loops and which one is better and makes more sense to implement ? Thanks ...

ForEach loop: Output something different on every second result

I have two for each loops and I am trying to output something different for each second result: foreach ($wppost as $wp) { $wp_title = $wp->post_title; $wp_date = strtotime($wp->post_date); $wp_slug = $wp->post_name; $wp_id = $wp->ID; // Start Permalink Template $wp...

Generating Unordered List with PHP + CodeIgniter from a MySQL Database

Hello Everyone, I am trying to build a dynamically generated unordered list in the following format using PHP. I am using CodeIgniter but it can just be normal php. This is the end output I need to achieve. <ul id="categories" class="menu"> <li rel="1"> Arts &amp; Humanities <ul> <li rel="2"> Photography ...

How do I use foreach with QDomNodeList in Qt?

Hi Everyone, I'm new to Qt and I'm learning something new every day. Currently, I'm developing a small application for my Nokia N900 in my free time. Everything is fine, I am able to compile and run Maemo applications on the device. I've just learned about the foreach keyword in Qt. (I know it is not in C++, so I didn't think about it ...

How to build boost foreach cycle

Hi guys! I have some abstract class called IClass (has pure virtual function). There are some classes which inherit IClass: CFirst, CSecond. I want to add objects of classes which inherit into boost::ptr_vector: class IClass { virtual void someFunc() = 0; }; class CFirst : public IClass { }; class CSecond : public IClass { }; boost::pt...

php foreach as variable

I'd like to use foreach to loop though an array list and add an element to each array. $tom = array('aa','bb','cc'); $sally = array('xx','yy','zz'); $myArrays = array('tom','sally'); foreach($myArrays as $arrayName) { ${$arrayName}[] = 'newElement'; } Is the use of ${$arrayName}[] the best way to do this? Is there another op...

Foreach loop and tasks.

I know from the codeing guidlines that I have read you should not do for (int i = 0; i < 5; i++) { Task.Factory.StartNew(() => Console.WriteLine(i)); } Console.ReadLine(); as it will write 5 5's, I understand that and I think i understand why it is happening. I know the solution is just to do for (int i = 0; i < 5; i++) { ...

When iterating over values, why does typeof(value) return "string" when value is a number? Javascript

I'm using Google Chrome for this test: Contrary to intuition, the first loop alerts "string" 3 times, while the second loop alerts "number" 3 times. numarray = [1, 2, 3]; //for-each loop for(num in numarray) alert(typeof(num)); //standard loop for(i=0; i<numarray.length; i++) alert(typeof(numarray[i])); I was expecting bot...

How to find the one Label in DataList that is set to True

In my .aspx page I have my DataList: <asp:DataList ID="DataList1" runat="server" DataKeyField="ProductSID" DataSourceID="SqlDataSource1" onitemcreated="DataList1_ItemCreated" RepeatColumns="3" RepeatDirection="Horizontal" Width="1112px"> <ItemTemplate> ProductSID: <asp:Label ID="ProductSIDLabel" runat="ser...

C# 4.0 'dynamic' and foreach statement

Not long time before I've discovered, that new dynamic keyword doesn't work well with the C#'s foreach statement: using System; sealed class Foo { public struct FooEnumerator { int value; public bool MoveNext() { return true; } public int Current { get { return value++; } } } public FooEnumerator Ge...