Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: map weirdness

by zejames (Hermit)
on Dec 13, 2004 at 13:09 UTC ( #414368=note: print w/replies, xml ) Need Help??


in reply to map weirdness

By the way, you are using
[@{$d[$mapkey]}]

which means that you have a array ref, $d{$mapkey} that you derefence through @{$d[$mapkey]}, and then you want to have the corresponding array ref. This is not useful, just use :

$d[$mapkey]

HTH


--
zejames

Replies are listed 'Best First'.
Re^2: map weirdness
by ysth (Canon) on Dec 13, 2004 at 13:33 UTC
    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.
      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}].
Re^2: map weirdness
by Fletch (Chancellor) on Dec 13, 2004 at 14:10 UTC

    That's not necessarily true. Doing this creates a new arrayref with the same contents, but if down the line he's manipulating the map'd contents but wants to be able to run more results through the same process he may very well want to have copied the original data (since if he alters the map'd contents down the line it'll affect what he's using to build the new list). It is more inefficient than just using the arrayref, but depending on what he's trying to do it might be correct.

Re^2: map weirdness
by insaniac (Friar) on Dec 13, 2004 at 13:16 UTC
    do more with less... i like, thanx dude! (again /me crawls away with his head as red as a tomato)
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame
      again /me crawls away with his head as red as a tomato

      Nothing to be ashamed of. You're learning. We all have to do that.

        yeah but it kinda bites my ass when i see that i could have found the answer myself :-)
        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame
Re^2: map weirdness
by Anonymous Monk on Dec 13, 2004 at 15:08 UTC
    Actually, [@{$d[$mapkey]}] and $d[$mapkey] are not the same. The first makes a copy, the second doesn't.
    #!/usr/bin/perl use strict; use warnings; my @d = ([0, 1, 2], [3, 4, 5], [6, 7, 8]); my $mapkey = 1; sub show { local($") = ", "; print("[", join(", ", map({"[@$_]"} @d)), "]\n"); } my $one = [@{$d[$mapkey]}]; my $two = $d[$mapkey]; show; # Show array. Unmodified. $one->[1] = "foo"; show; # Array is still unmodified. $two->[1] = "foo"; show; # This modifies the array. __END__ [[0, 1, 2], [3, 4, 5], [6, 7, 8]] [[0, 1, 2], [3, 4, 5], [6, 7, 8]] [[0, 1, 2], [3, foo, 5], [6, 7, 8]]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://414368]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-10-24 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (246 votes). Check out past polls.

    Notices?