Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

while going through perldsc tutorial...

by convenientstore (Pilgrim)
on Oct 16, 2007 at 19:33 UTC ( #645260=perlquestion: print w/replies, xml ) Need Help??
convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

From perldsc tutorial, I don't understand why below \@array is refering to the samething and code is wrong.. It says "all the ferences in @AoA refer to the very same place, and they will therefore all hold whatever was last in @array "
Trying to understand thist but if someone can explain...
@AoA = ( [2, 3], [4, 5, 7], [0] ); print $AoA[1][2]; 7 print @AoA; ARRAY(0x83c38)ARRAY(0x8b194)ARRAY(0x8b1d0) for $i (1..10) { @array = somefunc($i); $AoA[$i] = \@array; # WRONG! }

Replies are listed 'Best First'.
Re: while going through perldsc tutorial...
by philcrow (Priest) on Oct 16, 2007 at 19:41 UTC
    In the line marked #WRONG, the reference is always taken to @array which is only allocated once in the program (probably implicitly). Therefore, on each iteration, @array gets new values, and its location gets stored and all those references refer to the last set of values. To fix it, declare @array on each iteration:
    my @array = somefunc($i);
    Then, new memory is allocated each time and the references are to those different memory areas.

    Phil

    The Gantry Web Framework Book is now available.
      i understand now.. thank you
Re: while going through perldsc tutorial...
by naikonta (Curate) on Oct 17, 2007 at 08:31 UTC
    You keep getting the same memory address of @array. No matter how many times you iterate, since all elements of @AoA refer to the same memory address, they all share the same value of @array, whatever its last content. Other than making array @array lexical, you can also assign anonymous array reference. Consider this,
    #!/usr/bin/perl use strict; use warnings; ## first sample print "---- 1. global \@array ----\n"; my(@aoa, @array); for my $i (0..5) { @array = map { "#" x $_ } (0 .. $i); $aoa[$i] = \@array; print "$aoa[$i]\n"; # same memory address } print "@$_\n" for @aoa; ## second sample print "\n---- 2. lexical \@array ----\n"; my(@AoA); for my $i (0..5) { my @array = map { "#" x $_ } (0 .. $i); $AoA[$i] = \@array; print $AoA[$i], "\n"; # different memory address } print "@$_\n" for @AoA; ## third sample print "\n---- 3. anonymous array reference ----\n"; for my $i (0..5) { my @array = $AoA[$i] = [ map { "#" x $_ } (0 .. $i) ]; print $AoA[$i], "\n"; # different memory address } print "@$_\n" for @AoA;
    Output,
    ---- 1. global @array ---- ARRAY(0x814f618) ARRAY(0x814f618) ARRAY(0x814f618) ARRAY(0x814f618) ARRAY(0x814f618) ARRAY(0x814f618) # ## ### #### ##### # ## ### #### ##### # ## ### #### ##### # ## ### #### ##### # ## ### #### ##### # ## ### #### ##### ---- 2. lexical @array ---- ARRAY(0x818b03c) ARRAY(0x814ed48) ARRAY(0x8185ab8) ARRAY(0x8185aac) ARRAY(0x8185a58) ARRAY(0x8185a40) # # ## # ## ### # ## ### #### # ## ### #### ##### ---- 3. anonymous array reference ---- ARRAY(0x8183e38) ARRAY(0x818b03c) ARRAY(0x814ed48) ARRAY(0x8185ab8) ARRAY(0x8185aac) ARRAY(0x8185a58) # # ## # ## ### # ## ### #### # ## ### #### #####

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://645260]
Approved by GrandFather
help
Chatterbox?
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-21 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (118 votes). Check out past polls.

    Notices?