foreach

[Flex] XML HTTPService Result and for each...

Hi! XML is cool in flex, but I have an annoying problem to solve, caused by what I can imagine is a feature. You see, when you have just one tag of something, it creates an object which is not an array. But when finds more than one, it puts it on an array structure. I want to solve it elegantly. Any recommendation. Example: this illus...

Are there any side effects of returning from inside a foreach statement?

Similar to my question about returning from inside a using statement (whose answer was generally "yes, it's ok") I'm wondering if returning from inside a foreach statement is similarly devoid of side-effects and considered accepted practice, or when I do this am I leaving a pointer hanging in the middle an enumeration somewhere internall...

Looping through a JOIN'ed MySQL Associative Array

Right, so I've got a query that looks like this: $bestof_query = "SELECT * FROM physicians p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC "; $query = mysql_query($bestof_query); while($result = mysql_fetch_array($query)) { extract($result); ech...

PHP foreach over an array of objects

I'm trying to use a foreach loop for an array of objects. Inside of the BeginBattle() method I want to iterate through all of the objects and increment their played count automatically. Unfortunately, the web browser shows I have an error: Fatal error: Call to a member function BattleInitiated() on a non-object in /nfs/c05/h01/mnt/7029...

Is it better coding practice to define variables outside a foreach even though more verbose?

In the following examples: the first seems more verbose but less wasteful of resources the second is less verbose but more wasteful of resources (redefines string each loop) Which is better coding practice? First example: using System; using System.Collections.Generic; namespace TestForeach23434 { class Program { s...

Java: What does the colon (:) operator do?

I would look it up myself, but I don't even know what it's called. Would anyone mind explaining what it does? Thanks! EDIT: I didn't know there were multiple times the : appeared. What does it do in this case here: public String toString() { String cardString = ""; for (PlayingCard c : this.list) // <-- { cardString = cardString...

How can I access the next value in a collection inside a foreach loop in C#?

I'm working in C# and with a sorted List<T> of structs. I'm trying to iterate through the List and for each iteration I'd like to access the next member of the list. Is there a way to do this? Pseudocode example: foreach (Member member in List) { Compare(member, member.next); } ...

for iteration, element is undefined

I have this code: for(var i in this.units) { if(this.units[i].x==att.x && this.units[i].y==att.y){} //... some more code } and sometimes, randomly, I get an error this.units[i] is undefined. Anybody got any idea how this is possible? ...

Why is foreach so slow?

PHPBench.com runs quick benchmark scripts on each pageload. On the foreach test, when I load it, foreach takes anywhere from 4 to 10 times as long to run than the third example. Why is it that a native language construct is apparently slower than performing the logic oneself? ...

How does a lambda in C# bind to the enumerator in a foreach?

I just came across the most unexpected behavior. I'm sure there is a good reason it works this way. Can someone help explain this? Consider this code: var nums = new int[] { 1, 2, 3, 4 }; var actions = new List<Func<int>>(); foreach (var num in nums) { actions.Add(() => num); } foreach (var num in nums) { var x = num; act...

Java: Are these 2 codes the same?

for (Player p : players) { p.addCard(deck.dealCard()); p.addCard(deck.dealCard()); } and for (int i = 0; i < players.size() ; i++) { Player p = players.get(i); p.addCard(deck.dealCard()); p.addCard(deck.dealCard()); } The second code yeilds a null pointer exception, what can be done to make the bottom one equivale...

Java for each vs regular for -- are they equivalent?

Are these two constructs equivalent? char[] arr = new char[5]; for (char x : arr) { // code goes here } Compared to: char[] arr = new char[5]; for (int i = 0; i < arr.length; i++) { char x = arr[i]; // code goes here } That is, if I put exactly the same code in the body of both loops ...

Question about the foreach-value

Hello! I've found in a Module a for-loop written like this for( @array ) { my $scalar = $_; ... ... } Is there Difference between this and the following way of writing a for-loop? for my $scalar ( @array ) { ... ... } ...

For Each Loop LINQ Insert giving me Cannot add an entity that already exists.

I have the following code, that in my head should create a new row in a table: Dim fin As Boolean Dim db2 As New ChecklistModeldbmlDataContext For Each a In bServers If collection("fin" & a.ServerName) = "true, false" Or collection("fin" & a.ServerName) = "true,false" Then fin = True Else fin = False End If b...

VB.NET For Each steps into loop body for an IEnumerable collection! How? Why?

This is weird. I have a class that inherits from IEnumrable whose Count property is reporting 0 (zero) elements but the For Each loop steps into the loop body and tries to use the variable where it should just be moving on. My code: On Error Resume Next Dim d As Foo For Each d In fooCollection ' use d and throws an e...

Changing keys in a "for ( keys %hash ) {}"-loop

Hello! I remember something about not changing the keys in a for my $key ( keys %hash ) { ... for example for my $key ( keys %hash ) { $key = "$key_x"; } But deleting keys and changing values would be fine. Are my memories OK? ...

Getting an error on my foreach loop

Here is the code: <?php //Starting session session_start(); //Includes mass includes containing all the files needed to execute the full script //Also shows homepage elements without customs require_once ('includes/mass.php'); $username = $_SESSION['username']; if (isset($username)) { //Query database for the users networths ...

Using xsl:variable in a xsl:foreach select statment

I'm trying to iterate through an xml document using xsl:foreach but I need the select=" " to be dynamic so I'm using a variable as the source. Here's what I've tried: ... <xsl:template name="SetDataPath"> <xsl:param name="Type" /> <xsl:variable name="Path_1">/Rating/Path1/*</xsl:variable> <xsl:variable name="Path_2">/Rating/Path...

Why can't my Perl subroutine see the value for the variable in the foreach loop that called it?

I hope this is something straightforward that I'm doing wrong. I saw something online about "variable suicide" that looked good, but it was for an older version and I'm on 5.10.1. Anyway - a variable that I declared - $RootDirectory - just suddenly loses its value, and I can't figure out why. Here's a script to reproduce the problem....

How do I apply the for-each loop to every character in a String in Java?

So I want to iterate for each character in a string. So I thought: for (char c : "xyz") but I get a compiler error: StackCharTester.java:20: foreach not applicable to expression type How can I do this? ...