I think is same with "foreach" loop. So however they do it, perl's for-loops don't have the issue.
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
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).
function max(dict):
mx = ""
for each e in dict:
if e > mx then mx = e
end for
return mx
end function
"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).
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
"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?
my %hash = ( 1=>10, 2=>20 );
while (my ($key,$value) = each %hash) {
while (my ($key2,$value2) = each %hash) {
print "$key $value, $key2 $value2\n";
}
}
"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).
"RokuMarkn" wrote: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.
I was referring to the "each" statement, not for/foreach which indeed doesn't have the problem. However this fails catastrophically: