
Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2019
12:51 AM
Bug with nested FOR EACH blocks, internal counter gets messed up
I got some strange behavior of endless repeating, and I found out that it was due to two FOR EACH loops running (in different Functions)
The first Function runs a FOR EACH, and one of the operations inside calls another Function which calls another Function which has it's own FOR EACH that is pointing to the same list as the original FOR EACH is.
My solution was to change one of them to FOR i = 0 TO... format
Is this a known behavior that I should have been smart enough to avoid in the first place?
EDIT - I tested using FOR i = 0 TO in both FOR loops, and there is no problem, even with each using the same variable "i", so it's only when using FOR EACH and thus it uses Roku's internal counter system somehow.
The first Function runs a FOR EACH, and one of the operations inside calls another Function which calls another Function which has it's own FOR EACH that is pointing to the same list as the original FOR EACH is.
My solution was to change one of them to FOR i = 0 TO... format
Is this a known behavior that I should have been smart enough to avoid in the first place?
EDIT - I tested using FOR i = 0 TO in both FOR loops, and there is no problem, even with each using the same variable "i", so it's only when using FOR EACH and thus it uses Roku's internal counter system somehow.
4 REPLIES 4


Roku Employee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2019
05:30 PM
Re: Bug with nested FOR EACH blocks, internal counter gets messed up
"Komag" wrote:
...
EDIT - I tested using FOR i = 0 TO in both FOR loops, and there is no problem, even with each using the same variable "i", so it's only when using FOR EACH and thus it uses Roku's internal counter system somehow.
Correct. Each object that implements ifEnum (the enumeration interface) maintains a single internal position pointer. The 'for each' loop just initializes, checks, and advances that position via the object's ifEnum API. If you nest 'for each' loops on the same object, or call the ifEnum interface directly in-between, you will not get the 'expected' behavior.
While I would agree it's not intuitive, and I don't recall any clear documentation or examples off the top of my head, it's "always" worked that way.
And it's been discussed in this forum, I'm sure many years ago though, so I'm not going to go looking for it 😉
I encourage you to file a documentation enhancement request as well.

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2019
03:06 AM
Re: Bug with nested FOR EACH blocks, internal counter gets messed up
Okay, but I'm not sure how to do that.
The page is
https://developer.roku.com/docs/references/brightscript/language/program-statements.md
under
FOR EACH item IN object
"If a second use of FOR EACH is nested within the program statements of the original FOR EACH and points to the same "ifEnum" object, the internal ordinal keeping track of the list position will behave unexpectedly, so it's best to avoid this scenario, perhaps by making use of the FOR counter = exp TO exp format in at least one of them."
The page is
https://developer.roku.com/docs/references/brightscript/language/program-statements.md
under
FOR EACH item IN object
"If a second use of FOR EACH is nested within the program statements of the original FOR EACH and points to the same "ifEnum" object, the internal ordinal keeping track of the list position will behave unexpectedly, so it's best to avoid this scenario, perhaps by making use of the FOR counter = exp TO exp format in at least one of them."


Roku Employee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2019
04:43 PM
Re: Bug with nested FOR EACH blocks, internal counter gets messed up
The only route I know is for you to post on the "SDK documentation: links to fix, missing/broken/etc" topic (https://forums.roku.com/viewtopic.php?f=34&t=69731)
If you want to provide suggested text that's great, but if you just point out what's missing or not clear it should be their job to figure out how to address it.
YMMV. 😉
If you want to provide suggested text that's great, but if you just point out what's missing or not clear it should be their job to figure out how to address it.
YMMV. 😉

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2019
03:56 AM
Re: Bug with nested FOR EACH blocks, internal counter gets messed up
Thanks, I did that.