in reply to Re: map weirdness
in thread map weirdness

Those aren't the same; try:
$, = " "; $d[3] = [qw/just another perl hacker/]; $copy1 = $d[3]; $copy2 = [@{$d[3]}]; print "\$d[3] was", @{$d[3]}, "\n"; $d[3][2] =~ y/p/P/; print "\$d[3] now", @{$d[3]}, "\n"; print "\$copy1 is", @$copy1, "\n"; print "\$copy2 is", @$copy2, "\n"; __END__ $d[3] was just another perl hacker $d[3] now just another Perl hacker $copy1 is just another Perl hacker $copy2 is just another perl hacker
The [@{}] syntax copies the array elements into a new array, without it, you are sharing the same array.

Replies are listed 'Best First'.
Re^3: map weirdness
by insaniac (Friar) on Dec 13, 2004 at 14:52 UTC
    well, it works in my case..
    insaniac][amano: ~ : perl -we 'use strict;my @d=([0,"BE"],[3,"BUS"],[4 +,"BUS2"]);my @a=map($_ , split(/,/, "BUS2,BE") ) ; my @b=map { my $ma +pkey=$_; map { $d[$mapkey]->[1] =~ /$a[$_]/ ? $d[$mapkey] : () } 0..$ +#a } 0..$#d; use Data::Dumper;print Dumper(@b);' $VAR1 = [ 0, 'BE' ]; $VAR2 = [ 4, 'BUS2' ]; insaniac][amano: ~ : perl -we 'use strict;my @d=([0,"BE"],[3,"BUS"],[4 +,"BUS2"]);my @a=map($_ , split(/,/, "BUS2,BE") ) ; my @b=map { my $ma +pkey=$_; map { $d[$mapkey]->[1] =~ /$a[$_]/ ? [@{$d[$mapkey]}] : () } + 0..$#a } 0..$#d; use Data::Dumper;print Dumper(@b);' $VAR1 = [ 0, 'BE' ]; $VAR2 = [ 4, 'BUS2' ];
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame
      Add @d to the Dumper() call, and you'll see the difference:
      $ perl -we 'use strict;my @d=([0,"BE"],[3,"BUS"],[4, "BUS2"]);my @a=map($_ , split(/,/, "BUS2,BE") ) ; my @b=map { my $mapk +ey =$_; map { $d[$mapkey]->[1] =~ /$a[$_]/ ? $d[$mapkey] : () } 0..$#a } +0 ..$#d; use Data::Dumper;print Dumper(@d,@b)' $VAR1 = [ 0, 'BE' ]; $VAR2 = [ 3, 'BUS' ]; $VAR3 = [ 4, 'BUS2' ]; $VAR4 = $VAR1; $VAR5 = $VAR3; $ perl -we 'use strict;my @d=([0,"BE"],[3,"BUS"],[4, "BUS2"]);my @a=map($_ , split(/,/, "BUS2,BE") ) ; my @b=map { my $mapk +ey =$_; map { $d[$mapkey]->[1] =~ /$a[$_]/ ? [@{$d[$mapkey]}] : () } 0..$ +#a } 0 ..$#d; use Data::Dumper;print Dumper(@d,@b)' $VAR1 = [ 0, 'BE' ]; $VAR2 = [ 3, 'BUS' ]; $VAR3 = [ 4, 'BUS2' ]; $VAR4 = [ 0, 'BE' ]; $VAR5 = [ 4, 'BUS2' ];
      In the second case, @b is composed of refs to new arrays that have copies of the arrays referenced in @d. People often shoot themselves in the foot by leaving shared references like this in datastructures without meaning to. If, instead of 'BUS2' et. al., you had references, forming an even deeper structure, those would still be shared, though. If you need to prevent that, use something like Storable::dclone($ref) instead of [@{$ref}].
        thanx for your effort and explanation :-D
        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame