You have two loops dependent on the dimensions of the same array and only one control variable that is set up to exceed its proper bounds, where two iteration variables would work ok - the autovivication effect can better be thought of as a symptom of incorrect loop construction - i.e. don't try to address that problem on its own, but address the more abstract one of loop construction to solve this elegantly and maintainably.

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.:

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 }
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; $j <= $#$eventscores; $j++ ){ if ( $eventscores->[$j][0] == $eventscores->[$i][0] ) { push (@scores, $eventscores->[$j][2] ); } } # more code unrelated to loop }
__________________________________________________________________________________

^M Free your mind!


In reply to Re: $#{$array_ref} changes in loop by Moron
in thread $#{$array_ref} changes in loop by thomc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.