in reply to $#{$array_ref} changes in loop
There are a number of rules for constructing loops safely in any language that all apply here. Another rule is:
Don't modify any of the control variables from within a loop, whether it be the iterator or the start and stop limits or the compiler or interpreter is apt to stumble and behave unpredictably".
It takes experience to know more such rules but they relate to loop construction technique more than anything else. Three of the lines of code look functionally to be exactly the three parts of a 3-arg for loop, so better just write that, e.g.:
Update: or if you didn't really want to exit the innerloop on a non match (I find that functionally a bit weird though you might possibly be right about it) ...for my $i ( 0 .. $#$eventscores ) { # forces readonly for ( my $j = $i; $eventscores->[$j][0] == $eventscores->[$i][0]; $j++ ){ push (@scores, $eventscores->[$j][2] ); } # more code unrelated to loop }
for my $i ( 0 .. $#$eventscores ) { # forces readonly for ( my $j = $i; $j <= $#$eventscores; $j++ ){ if ( $eventscores->[$j][0] == $eventscores->[$i][0] ) { push (@scores, $eventscores->[$j][2] ); } } # more code unrelated to loop }
^M Free your mind!
|
|---|