
Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2015
05:14 AM
FOR EACH bug - one iteration breaks another
I was diagnosing a game bug and came across this behavior, which seems like a bug to me.
The FOR EACH loop breaks! It never does blue!
Why would printing a break the larger loop? Why does iterating through a to print it break the overall iteration of a for the FOR EACH loop? Is that a bug or is that common/known behavior across many programming languages?
BrightScript Debugger> a = ["red", "blue"]
BrightScript Debugger> ? a
red
blue
BrightScript Debugger> for each col in a: ? "color " col: end for
color red
color blue
BrightScript Debugger> for each col in a: ? "color " col: ? "test " a: end for
color red
test red
blue
BrightScript Debugger>
The FOR EACH loop breaks! It never does blue!
Why would printing a break the larger loop? Why does iterating through a to print it break the overall iteration of a for the FOR EACH loop? Is that a bug or is that common/known behavior across many programming languages?
5 REPLIES 5
adamkaz
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2015
07:36 AM
Re: FOR EACH bug - one iteration breaks another
Similarly interesting behavior:
BrightScript Debugger> for each col in a: ? [a[0],a[1]]:end for
red
blue
red
blue
BrightScript Debugger> for each col in a: ? a:end for
red
blue
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2015
09:02 AM
Re: FOR EACH bug - one iteration breaks another
"Komag" wrote:
I was diagnosing a game bug and came across this behavior, which seems like a bug to me.BrightScript Debugger> a = ["red", "blue"]
BrightScript Debugger> ? a
red
blue
BrightScript Debugger> for each col in a: ? "color " col: end for
color red
color blue
BrightScript Debugger> for each col in a: ? "color " col: ? "test " a: end for
color red
test red
blue
BrightScript Debugger>
The FOR EACH loop breaks! It never does blue!
Why would printing a break the larger loop? Why does iterating through a to print it break the overall iteration of a for the FOR EACH loop? Is that a bug or is that common/known behavior across many programming languages?
You're probably confusing the counter by referencing the array that way.
This code achieves the desired result -
Function Main()
a=["red","blue"]
for each col in a
print "color " col
end for
for each col in a
print "color" col
end for
for each col in a
print "test" col
end for
End Function
Produces this output -
color red
color blue
colorred
colorblue
testred
testblue
It is proper behavior according to other languages --
http://stackoverflow.com/questions/3307 ... in-foreach

RokuMarkn
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2015
10:33 AM
Re: FOR EACH bug - one iteration breaks another


Roku Employee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2015
10:54 AM
Re: FOR EACH bug - one iteration breaks another
Yes. Currently each enumerable object has a single iteration pointer associated with it.
So if within a 'for each' loop you nest another 'for each' loop, directly call the object's ifEnum functions Reset or Next, print the object (which internally iterates over the object), or similarly call FormatJSON, it will affect the outer iterator.
So if within a 'for each' loop you nest another 'for each' loop, directly call the object's ifEnum functions Reset or Next, print the object (which internally iterates over the object), or similarly call FormatJSON, it will affect the outer iterator.

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2015
05:06 AM
Re: FOR EACH bug - one iteration breaks another
Ouch, I'll have to be really careful about that then, drat. 😞