in reply to declare my variable in loop

The @Array_Vals goes out of scope as (each iteration of) the loop ends. If you use strict (and you always should - especially while learning) then that code won't even compile.

Replies are listed 'Best First'.
Re^2: declare my variable in loop
by gauss76 (Scribe) on Aug 08, 2017 at 08:33 UTC

    Apologies, the last curly bracket was in the wrong place. It should have been:

    my @Array_of_ALL_Vals; for my $iloop (0 .. 10){ my @Array_Vals; . Code in here to populate @Array_of_Vals . push @Array_of_ALL_Vals,\@Array_Vals; }

      With your code, the my declaration will create a fresh @Array_Vals every trip around the loop.

      If you want to see your data structure, use Data::Dumper to show it:

      my @Array_of_ALL_Vals; for my $iloop (0 .. 10){ my @Array_Vals; . Code in here to populate @Array_of_Vals . push @Array_of_ALL_Vals,\@Array_Vals; print Dumper \@Array_of_ALL_Vals; }

        OK thanks, that I understand, however on creation of the new @Array_Vals and subsequent inclusion into the @Array_of_ALL_Vals array will that mess up any of the previous values already present in the @Array_of_ALL_Vals array which would already contain the address of another instance of the @Array_Vals array which has now been overwritten.

      ... the last curly bracket was in the wrong place. It should have been ...

      It's possible and preferable for you to correct your OPwith a note to explain that a correction had been made; see How do I change/delete my post? See the Edit link in the upper right corner of your OP or in the lower right set of links in one of your replies.


      Give a man a fish:  <%-{-{-{-<