Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: declare my variable in loop

by gauss76 (Scribe)
on Aug 08, 2017 at 08:33 UTC ( [id://1196957]=note: print w/replies, xml ) Need Help??


in reply to Re: declare my variable in loop
in thread declare my variable in loop

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; }

Replies are listed 'Best First'.
Re^3: declare my variable in loop
by Corion (Patriarch) on Aug 08, 2017 at 08:35 UTC

    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.

        No.

        Your talk of "memory addresses" suggests to me that you are coming from a C mindset to Perl. I will try to explain this in terms of malloc:

        Think of my as malloc:

        SV* Array_Vals = malloc();

        Think of push @Array_of_ALL_Vals, \@Array_Vals as Array_of_ALL_Vals[ iloop ] = Array_Vals;.

        Then your Perl code looks like this translated to "C":

        AV* Array_of_ALL_Vals; for my $iloop (0 .. 10){ SV* Array_Vals = malloc(); // actually, newAV() . Code in here to populate @Array_of_Vals . Array_of_ALL_Vals[ iloop ] = Array_Vals; }

        Deallocating that memory again is some of the magic that Perl provides.

        ... the address of another instance of the @Array_Vals array which has now been overwritten.

        No. Each lexical variable created in the scope of the for-loop is independent of all the others; it has an independent address. Confirm this for yourself by looking (as Corion suggested) at these referenced arrays with Data::Dumper or some similar utility (I like Data::Dump).

        c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my @source = qw(a b c foo bar baz fee fie foe); ;; my @Array_of_ALL_Vals; ;; for my $iloop (0 .. 3){ my @Array_Vals; @Array_Vals = @source[ 0 .. rand @source ]; push @Array_of_ALL_Vals,\@Array_Vals; print Dumper \@Array_of_ALL_Vals; } ;; print '-----------'; print Dumper \@Array_of_ALL_Vals; " $VAR1 = [ [ 'a', 'b', 'c', 'foo', 'bar' ] ]; $VAR1 = [ [ 'a', 'b', 'c', 'foo', 'bar' ], [ 'a', 'b', 'c' ] ]; $VAR1 = [ [ 'a', 'b', 'c', 'foo', 'bar' ], [ 'a', 'b', 'c' ], [ 'a', 'b', 'c', 'foo' ] ]; $VAR1 = [ [ 'a', 'b', 'c', 'foo', 'bar' ], [ 'a', 'b', 'c' ], [ 'a', 'b', 'c', 'foo' ], [ 'a', 'b', 'c', 'foo', 'bar', 'baz', 'fee' ] ]; ----------- $VAR1 = [ [ 'a', 'b', 'c', 'foo', 'bar' ], [ 'a', 'b', 'c' ], [ 'a', 'b', 'c', 'foo' ], [ 'a', 'b', 'c', 'foo', 'bar', 'baz', 'fee' ] ];


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

Re^3: declare my variable in loop
by AnomalousMonk (Archbishop) on Aug 08, 2017 at 10:14 UTC
    ... 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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1196957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-18 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found