http://www.perlmonks.org?node_id=115124


in reply to A C-like brain in a Perl-like world

my @merged = values %{{ map {$_ => $_ } @array1,@array2 }} ;
The double-curlies are necessary - with single-curlies the interpreter whines and dies. If anyone can explain why, I'd be interested.

andy.

  • Comment on one-liner to merge arrays (Re: A C-like brain in a Perl-like world)
  • Download Code

Replies are listed 'Best First'.
(jeffa) Re: one-liner to merge arrays
by jeffa (Bishop) on Sep 27, 2001 at 23:25 UTC
    The reason why is because your map is evaluating into a hash reference, not a hash - values take a hash for it's argument, not a hash reference. Now, if you use a tempory hash reference instead, the reason for the double curlies becomes much more apparent:
    my $hash = { map {$_ => $_ } @a,@b }; my @merged = values %{ $hash };
    Good question, by the way :)

    jeffa

Re: one-liner to merge arrays (Re: A C-like brain in a Perl-like world)
by extremely (Priest) on Sep 27, 2001 at 23:42 UTC
    Lets look at it blown out:
    = values %{{ map {$_ => $_ } @a,@b }} ; = values # I want a hash and return a list %{ } # I turn a hashref into a hash { } # I turn a list into a hashref map { } @a,@b # I return a list from a list => , # We construct lists

    HTH...

    --
    $you = new YOU;
    honk() if $you->love(perl)

      Thanks extremely and jeffa, it's all clear now.

      I should have realised, since the array equivalent would be @{[  ...  ]} , that the brackets were doing different things - but I'd managed to confuse myself.

      Cheers,
      Andy.