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!


In reply to Re: while going through perldsc tutorial... by naikonta
in thread while going through perldsc tutorial... by convenientstore

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.