adeirossi has asked for the wisdom of the Perl Monks concerning the following question:
I would expect the above script to print: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"; }
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*** @aoa1 *** - - - - @ - - - - *** @aoa2 *** - - - - @ - - - -
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem Defining AoA from Named AoA
by bsdz (Friar) on Jan 13, 2007 at 11:28 UTC | |
|
Re: Problem Defining AoA from Named AoA
by bart (Canon) on Jan 13, 2007 at 12:01 UTC | |
|
Re: Problem Defining AoA from Named AoA
by madbombX (Hermit) on Jan 13, 2007 at 16:45 UTC |