Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm a Perl beginner having trouble with arrays of arrays. When I define an AoA using a named AoA, both array names seem to refer to the same AoA. For example, if I first define @aoa1 with an anonymous AoA and then define @aoa2 with "my @aoa2 = @aoa1;", the modification of @aoa2 undesirably modifies @aoa1 (and vice versa).

The script below demonstrates this problem:
use strict; use warnings; my @aoa1 = ( # define @aoa1 w/ anonymous AoA ["-","-","-"], ["-","-","-"], ["-","-","-"], ); # my @aoa2 = ( # define @aoa2 w/ anonymous AoA # ["-","-","-"], # ["-","-","-"], # ["-","-","-"], # ); my @aoa2 = @aoa1; # define @aoa2 w/ named AoA # $aoa1[1][1] = '@'; # modify @aoa1 $aoa2[1][1] = '@'; # modify @aoa2 printAoa( "aoa1", @aoa1 ); # print @aoa1 printAoa( "aoa2", @aoa2 ); # print @aoa2 exit; sub printAoa { my ( $aoaName, @aoa ) = @_; my $row; my $col; print "*** $aoaName ***\n"; for $row ( 0 .. 2 ) { for $col ( 0 .. 2 ) { print "$aoa[$row][$col] "; } print "\n"; } print "\n"; }
I would expect the above script to print:
*** @aoa1 *** - - - - - - - - - *** @aoa2 *** - - - - @ - - - -
Instead, I get:
*** @aoa1 *** - - - - @ - - - - *** @aoa2 *** - - - - @ - - - -
It's as if @aoa1 and @aoa2 refer to the same AoA. When "$aoa2[1][1] = '@';" is commented out and "$aoa1[1][1] = '@';" is activated, the output is the same.

The only way I can get @aoa1 and @aoa2 to behave as I'd like is to define both of them with anonymous AoA's. If you comment out "my @aoa2 = @aoa1;" and uncomment the lines above it, the script produces the desired output. This is all well and good, but in my actual script, I need to define AoA's from named AoA's rather than anonymous ones.

Any suggestions would be greatly appreciated.

Thanks,
Andrew

In reply to Problem Defining AoA from Named AoA by adeirossi

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found