Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
EnTerr
Level 11

Bug in "for each"

Is this a known bug? It sure is not documented in lang-ref.
The following is expected to print multiplication table (81 rows):
BrightScript Debugger> nums = [1,2,3,4,5,6,7,8,9]
BrightScript Debugger> for each i in nums: for each j in nums: ? i " x" j " =" i*j: end for: end for
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
0 Kudos
14 REPLIES 14\
TheEndless
Level 10

Re: Bug in "for each"

Not sure that's a bug. For Each uses the ifEnum interface of the array to loop through the items. Since you're looping through the same array in both loops, by the time "i" is ready to move next, the enumerator is already at the end of the array.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
destruk
Level 10

Re: Bug in "for each"

This works fine --

nums = [1,2,3,4,5,6,7,8,9]
nums2 = [1,2,3,4,5,6,7,8,9]
for each i in nums:for each j in nums2:? i " x" j " =" i*j:end for:end for


which is what TheEndless said for a solution.
0 Kudos
EnTerr
Level 11

Re: Bug in "for each"

"destruk" wrote:
This works fine --

nums = [1,2,3,4,5,6,7,8,9]
nums2 = [1,2,3,4,5,6,7,8,9]
for each i in nums:for each j in nums2:? i " x" j " =" i*j:end for:end for

DUH: It will also work fine if (a) i use "regular" for loop or (b) don't use loop at all but hard-code the table or (c) start growing water lilies instead of trying to code in B/S.
But this is not the point!!
It was just an ILLUSTRATION of something that works in any sane language with "for each" loop that i can think of (and i dare you to find example to the contrary).
When program gets complicated enough, there is no way to predict who will enumerate when and therefore behavior gets unpredictable.
0 Kudos
destruk
Level 10

Re: Bug in "for each"

I tried a regular for loop, and I agree that works. But I didn't try hard coding the table. I'll take your word for that working. Smiley Happy -- that was intended as a joke...
I'd also like them to change the reference system so that num=nums actually makes a duplicate of nums in ram under the num variable, and I'd like to request less stringent case checking, and more support for global variables aside from the AA or m reference. Maybe they will fix the enumerator in "for each" - but if time permits they could work on my other requests? It would make the language so much easier for me to work with if I didn't have to do so much type-specific conversion and use other extended/length baby gloves code routines. I'd think after all these years Roku has been in business, they would enable profile pictures/avatars on the forum and a spam blocker or at least add a captcha....
0 Kudos
NewManLiving
Level 8

Re: Bug in "for each"



l_rows = [1,2,3,4,5,6,7,8,9]
l_cols = [1,2,3,4,5,6,7,8,9]

for each l_i in l_rows

print "======================"

for each l_j in l_cols
print l_i;" x "; l_j;" = "; l_i * l_j
end for

end for
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
RokuMarkn
Level 8

Re: Bug in "for each"

"EnTerr" wrote:

It was just an ILLUSTRATION of something that works in any sane language with "for each" loop that i can think of (and i dare you to find example to the contrary).


Perl's "each" statement has the same issue that only one iterator can be active at a time in a given hash, for the same reason.

--Mark
0 Kudos
NewManLiving
Level 8

Re: Bug in "for each"

Sorry I copied in my code without my comment
But as to what was pointed out: for each apparently resolves to a pointer
To the enumerator interface of the object and not to a separate iteration class
If as in my example you did l_cols = l_rows you would get the same result
So unfortunately as you know you need to create two
Your own iteration class
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
TheEndless
Level 10

Re: Bug in "for each"

"NewManLiving" wrote:
Sorry I copied in my code without my comment
But as to what was pointed out: for each apparently resolves to a pointer
To the enumerator interface of the object and not to a separate iteration class
If as in my example you did l_cols = l_rows you would get the same result
So unfortunately as you know you need to create two
Your own iteration class

Probably makes more sense, in this case, to do a "For i = 0 To nums.Count() - 1" instead of a "For Each" with two copies of the same data.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
NewManLiving
Level 8

Re: Bug in "for each"

Very true but I did not get the impression he was
Very happy about that idea. By the way did you
Get a chance to look at the virtual list view
Example I was hoping you would look at it
It's posted in the wisdom post
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos