in reply to Re^3: map weirdness
in thread map weirdness

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}].

Replies are listed 'Best First'.
Re^5: map weirdness
by insaniac (Friar) on Dec 15, 2004 at 15:24 UTC
    thanx for your effort and explanation :-D
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame