Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
EnTerr
Roku Guru

Re: Bug in "for each"

Mark - I have not touched Perl in almost ten years but this seems to work?

DB<21> @x = qw(a b c);
DB<28> for my $i (@x) { for my $j (@x) { print "$i, $j\n" }}
a, a
a, b
a, c
b, a
b, b
b, c
c, a
c, b
c, c
I think is same with "foreach" loop. So however they do it, perl's for-loops don't have the issue.

Perhaps a better example of how this is dangerous:

function max(dict):
mx = ""
for each e in dict:
if e > mx then mx = e
end for
return mx
end function
This tame function finds and returns the biggest key from a roAA (or biggest element from a list/array of strings). Except it turns out to be destructive and will ruin loops over the hash (when called from inside those loops - no matter how many levels of function call nesting).

And here is the rub: i cannot think of reasonable alternative way to implement that function in BRS, since roAA has no "keys()" to get me array of the keys (or a safe iterator).
PS. added "reasonable" above (nod to Bill Nye's arguing creationism recently with Ken Ham) to skirt advice of copying the hash on each call to the fn (consider len(dict)>100000, times thousands of calls).
0 Kudos
TheEndless
Channel Surfer

Re: Bug in "for each"

"EnTerr" wrote:
And here is the rub: i cannot think of alternative way to implement that function in BRS, since roAA has no "keys()" to get me array of the keys (or a safe iterator).

Not ideal, but...
function max(dict):
    mx = ""
copy = {}
copy.Append(dict)
    for each e in copy:
        if  e > mx then mx = e
    end for
    return mx
end function
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
RokuMarkn
Visitor

Re: Bug in "for each"

"EnTerr" wrote:
"RokuMarkn" wrote:
"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 - I have not touched Perl in almost ten years but this seems to work?

I was referring to the "each" statement, not for/foreach which indeed doesn't have the problem. However this fails catastrophically:

my %hash = ( 1=>10, 2=>20 );
while (my ($key,$value) = each %hash) {
while (my ($key2,$value2) = each %hash) {
print "$key $value, $key2 $value2\n";
}
}

This goes into an infinite loop printing "1 10, 2 20" forever.

--Mark
0 Kudos
RokuMarkn
Visitor

Re: Bug in "for each"

"EnTerr" wrote:

Perhaps a better example of how this is dangerous:


function max(dict):
mx = ""
for each e in dict:
if e > mx then mx = e
end for
return mx
end function


This tame function finds and returns the biggest key from a roAA (or biggest element from a list/array of strings). Except it turns out to be destructive and will ruin loops over the hash (when called from inside those loops - no matter how many levels of function call nesting).

And here is the rub: i cannot think of alternative way to implement that function in BRS, since roAA has no "keys()" to get me array of the keys (or a safe iterator).


That's a very good example. I will write an enhancement request for a better way to handle this.

--Mark
0 Kudos
EnTerr
Roku Guru

Re: Bug in "for each"

"RokuMarkn" wrote:
I was referring to the "each" statement, not for/foreach which indeed doesn't have the problem. However this fails catastrophically:
I see the use of "each" operator is discouraged in perl or at least there are warnings about it. Resetting their iterator also looks clumsy.

But how come Perl's for/foreach loops work fine?
My specific gripe was 'that works in any sane language with "for each" loop that i can think of' - and seems even Perl has not failed that.
So in a way perl's "each" is BRS "ifEnum". There is no reason why "for each" should suffer the issues of ifEnum (just like perl's "foreach" does not have "each"s problems)

PS. in retrospective (20/20) seems a better way would have been if roList/roAA/roArray had method getEnum() which returns iterator object complying with ifEnum, rather than them directly implementing ifEnum. This way each new call to getEnum() could return new iterator.

PPS. @RokuMarkn, just saw the second reply; will restrain beating further the horse.
0 Kudos