foreach

Lock vs. ToArray for thread safe foreach access of List collection

I've got a List collection and I want to iterate over it in a multi threaded app. I need to protect it every time I iterate it since it could be changed and I don't want "collection was modified" exceptions when I do a foreach. What is the correct way to do this? Use lock every time I access or loop. I'm rather terrified of deadlock...

Print Terms in Node in Multiple Columns Sorted in Downward Order

I have approximately 20 content-taxonomy check boxes from one field ("features"). The checked terms display in node-example.tpl.php. I am trying to show these content-taxonomy terms in two columns displayed/sorted in a downward order instead of across. I am trying to cobble two bits of code to accomplish this...but my php skills are not...

Getting a foreach to run once

So the question is this, I have a for each loop that I am currently using to retrieve data from a query of an XML file which then gets put into a string; like so: foreach (var value in dateQuery) date = value.Element("HistoryCreation").Value; I know for a fact (Based on the way the xml file stores values and the query ...

List ForEach break

is there a way to break out of the foreach extension method? The "break" keyword doesn't recognize the extension method as a valid scope to break from. //Doesn't compile Enumerable.Range(0, 10).ToList().ForEach(i => { System.Windows.MessageBox.Show(i.ToString()); if (i > 2)break; }); Edit: removed "linq" from question note the c...

Unit testing various foreach of some inputs

Especially for C# Unit Testing, how to make a test run for various inputs or an input matrix? I very often have the case of unit tests making a matrix of inputs, but usually it can be reasonably solved by checking a list of inputs. The simplest case is testing the addition: 0 + 0 = 0 1 + 0 = 1 0 + -1 = -1 1 + -1 = 0 The same tests ...

Linq statement faster than foreach loop

I am writing a Mesh Rendering manager and thought it would be a good idea to group all of the meshes which use the same shader and then render these will I'm in that shader pass. I am currently using a foreach loop, but wondered if utilising Linq might give me a performance increase? ...

PHP Array manipulation

I have an array like this: [0] => Array ( [slideritem] => 592 [sliderbig] => 644 ) [1] => Array ( [slideritem] => 593 [sliderbig] => 645 ) [2] => Array ( [slideritem] => 594 [sliderbig] => 646 ) slideritem is the id of an image that will be displayed and the sli...

ActionScript 3: how to create a array or list like: array["name1"] = "hello" (just like you can do in PHP)

hello, in PHP i am used to create a array with using names as keys like array["something1"] = "output1"; array["something2"] = "output2"; array["something3"] = "output3"; and then use foreach to let them print or do other things with it like foreach ($array as $key => $value) { echo "$key = $value"; } is there something similar po...

Better way to implement filtered enumerator on TList<TMyObject>

Using Delphi 2010, let's say I've got a class declared like this: TMyList = TList<TMyObject> For this list Delphi kindly provides us with an enumerator, so we can write this: var L:TMyList; E:TMyObject; begin for E in L do ; end; The trouble is, I'd like to write this: var L:TMyList; E:TMyObject; begin for E in L.GetEn...

Variables are not being rewritten as page is processed.

I have a php script that checks to see if a particular file exists. This name of the file is defined by the 'compartment' variable. When the script is copied and pasted again into a separate block, changing only the compartment variable it runs into a problem... Say for example 1.jpeg exists but 2.jpeg doesn't. The first block displays ...

Cycle categories with subcategories

1) i have 2 tabs Categories and SubCategories on db with relation 1 to many (im using entity framework) 2) i have to create a vertical menu like this <ul> <li>category 1 <ul> <li>subcategory 1</li> <li>subcategory 2</li> <li>subcategory 3</li> </ul> </li> </ul> i think that my problem is here ...

each function jquery. is it correct??

$('.dragbox').each(function(){ $('.close').click(function(){ $(this).parent().hide(); }), $('.colpase').click(function(){ $(this).siblings('.dragbox_content').toggle(); }) }); ...

Multi-threading on a foreach loop?

I want to process some data. I have about 25k items in a Dictionary. IN a foreach loop, I query a database to get results on that item. They're added as value to the Dictionary. foreach (KeyValuePair<string, Type> pair in allPeople) { MySqlCommand comd = new MySqlCommand("SELECT * FROM `logs` WHERE IP = '" + pair.Key + "' GROUP BY s...

How to get Resharper to convert back to a foreach loop

Resharper 5 can convert my foreachloops to Linq queries. Which I like. But linq is way way way harder to debug than a foreachloop. When I convert my foreach statement to a linq query, I don't see any option to go back the other way. Does any one know how to do this? Is it even possible? ...

ASP.NET/VB Iterate Through Collection, Do..This on Result Found, Do..That on Result Not Found.

[This is a simplified example] I have a collection ("myCollection") in which exists three entries: ("hello", "goodbye", "welcome"). I want to iterate through the collection and if in the collection there is the entry "welcome" I want to take one action, if that entry doesn't exist I want to do something else. Like this (pseudo): For Ea...

perl print array from subroutine

#! /usr/local/bin/perl sub getClusters { my @clusters = `/qbo/bin/getclusters|grep -v 'qboc33'`; chomp(@clusters); return \@clusters; } ummm okay .. how do I get at this array to print since ... foreach $cluster (getClusters()) { print $cluster."\n"; } doesn't seem to work. Thanks. ...

rails - building a line of code from a foreach loop

Hey, I am trying to create a intersection statement using a foreach loop for example cand[0][1,2,5] cand[1][2,5,6] @result = cand[0] & cand[1] with a for each intersec = Array.new cand.each do |c| intersec = intersec & c end @result = intersec I get an empty array Thanks Alex ...

PHP Calculate Total of Looped Items

I have this code: foreach ($cartContents as $item => $itemQty) echo "$item <br /> $itemQty <br /> $price"; It loops through some items and prints the name, quantity and price. I would then like to print a total of all the prices added together. Is there a way to get this figure? ...

I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?

I'm starting with 2010-05-01 and ending with 2010-05-10... how can I iterate through all of those dates? ...

What's wrong with this ForEach loop?

Yep... it's one of those days. public string TagsInput { get; set; } //further down var tagList = TagsInput.Split(Resources.GlobalResources.TagSeparator.ToCharArray()).ToList(); tagList.ForEach(tag => tag.Trim()); //trim each list item for spaces tagList.ForEach(tag => tag.Replace(" ", "_")); //replace remaining inner word spacings wit...