foreach

C# Linq statements or a foreach() for totalling subsets?

Which of these solutions is preferred? For a List: List<ExampleInfo> exampleList = new List<ExampleInfo>(); public class ExampleInfo { internal ExampleInfo() { } /* Business Properties */ public int Id { get; set; } public string Type { get; set; } public decimal Total { get; set; } } I wish to get subtotal...

"Nested foreach" vs "lambda/linq query" performance(LINQ-to-Objects)

In performance point of view what should you use "Nested foreach's" or "lambda/linq queries"? ...

php print array using nested foreach

I would like to ask how to print out this array with organize way This is my array > Array ( > [item1 under Category_1] => Category_1 > [item2 under Category_1] => Category_1 > [item3 under Category_2] => Category_2 > [item4 under Category_3] => Category_3 > [item5 under Category_3] => Category_3 > [item6 under...

C# "cannot assign field because it is a foreach iteration variable"

Hi all, I want to trim the column headers of a CSV file, fields below is an array that contains the header names, where would I place trim()? I've placed it with the foreach loop but VS tells me "cannot assign field because it is a foreach iteration variable". What am I doing wrong? while ((fields = csv.GetCSVLine()) != null) { if...

Iteration in JQuery

Hi, I am new to jQuery. I am having a variable increment in my code which shows how many divs inn mne of the panel. I want to iterate through those divs to get the properties of the labels, input type ,size everything inside each div. Starting from 1, i want to iterate up to increment .. How can i do so in JQUery? Please give me a su...

What does this code do? (2)

I don't understand the => part. foreach ($_POST[‘tasks’] as $task_id => $v) { What does it do in a foreach loop? ...

c# modifying structs in a List<T>

Short question: How can I modify individual items in a List? (or more precisely, members of a struct stored in a List?) Full explanation: First, the struct definitions used below: public struct itemInfo { ...(Strings, Chars, boring)... public String nameStr; ...(you get the idea, nothing fancy)... public String subNum; //BTW t...

Identifying last loop when using for each

I want to do something different with the last loop iteration when performing 'foreach' on an object. I'm using Ruby but the same goes for C#, Java etc. list = ['A','B','C'] list.each{|i| puts "Looping: "+i # if not last loop iteration puts "Last one: "+i # if last loop iteration } The output desired is equivalent to: ...

checkbox array loop in c#

I am receiving long string of checked html checkbox values (Request.Form["mylist"] return Value1,Value2,Value3....) on the form post in ASP.NET 2.0 page. Now I simply want to loop these but I don't know what is the best practice to loop this array of string. I am trying to do something like this: foreach (string Item in Request.Form["...

Terminology when copying the captured variable for a lambda/ anon-method

I translated this code(it has bad side effect that it just capture the outer variable): foreach (TaskPluginInfo tpi in Values) { GenerateMenu(menuStrip, tpi.MenuTree, tpi.MenuText, delegate { tpi.ShowTask() }); } To this code(because the above is not working): foreach (TaskPluginInfo tpi in Values) { ...

Can one do a for each loop in java in reverse order?

I need to run through a List in reverse order using Java. So where this does it forwards: for(String string: stringList){ //...do something } Is there some way to iterate the stringList in reverse order using the for each syntax? For clarity: I know how to iterate a list in reverse order but would like to know (for curiosity's sake...

Foreach-statement in c++ and language extension?

You can simulate foreach-statement in c++ with macro declaration. I'm using similar syntax for looping arrays in the following way: int array1[10]; vector<int> array2(10); fori(array1) forj(array2) fork(123) if(array1[i]==array[j]) return true; What's your favorite macros for extending c++ l...

excel vba for each loop takes 2 steps at once

I want to process every element of a for-loop. Taking this code, why is just every second element processed? For Each row In ws.Rows If IsEmpty(row.Cells(row.row, 1)) Then Exit For Else MsgBox row.Cells(row.row, 1).value End If Next Thanks in advance for your answers! ...

Using Python to execute a command on every file in a folder

Hello everyone, I'm trying to create a Python script that would : Look into the folder "/input" For each video in that folder, run a mencoder command (to transcode them to something playable on my phone) Once mencoder has finished his run, delete the original video. That doesn't seem too hard, but I suck at python :) Any ideas on wh...

Why doesn't a foreach loop work in certain cases?

Hello good people, I was using a foreach loop to go through a list of data to process (removing said data once processed--this was inside a lock). This method caused an ArgumentException now and then. Catching it would have been expensive so I tried tracking down the issue but I couldn't figure it out. I have since switched to a for l...

combinations: avoiding multiple nested foreach

When you need to check/have combinations of array elements, how can you avoid nesting foreach? Example code: $as = array($optionA1, $optionA2) $bs = array($optionB1, $optionB2) $cs = array($optionC1, $optionC2) foreach ($as as $a) { foreach ($bs as $b) { foreach ($cs as $c) { $result = $this->method($a, $b, $c); ...

Inconsistent behavior on java's for-each

Consider this code: class Jm44 { public static void main(String args[]){ int []arr = {1,2,3,4}; for ( int i : arr ) { arr[i] = 0; } for ( int i : arr ) { System.out.println(i); } } } It prints: 0 0 3 0 What's this? For-each is supposed to run over all the el...

Multidimensional array only returning one item in PHP

I have a multidimensional array, that is a few levels deep. I am trying to loop through some of the lower levels of array items, but when I do, it seems to only return one of the array items. foreach ($items as $item) { foreach ($item as $id) { echo $id; } } For some reason, echoing $id only returns the first item in the $ite...

C# newbie: find out the index in a foreach block

Hi, I have a foreach block where I want to plot out for trace-debug purposes the index of the step inside the foreach. As a C# newbie I do it as follows: int i = 1; foreach (x in y) { ... do something ... WriteDebug("Step: "+i.ToString()); i++; } I wondered if there's any way to get the value of the current step's index ...

Calling remove in foreach loop in Java

In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: List<String> names = .... for (String name : names) { // Do something names.remove(name). } As an addendum, is it legal to remove items that have not been iterated over yet? For instance, //Assume that...