in reply to Re: How to connect more arrays to a new array
in thread Find unique elements from multiple arrays

my @new = keys %{{ map +($_ => undef), @a, @b, @c, @d }};
It should be noted that the difference to the grep solution is that this won't preserve any kind of order.

Makeshifts last the longest.

  • Comment on Re^2: How to connect more arrays to a new array (hash, without temporaries)
  • Download Code

Replies are listed 'Best First'.
Re: Re^2: How to connect more arrays to a new array
by BrowserUk (Patriarch) on Apr 28, 2003 at 08:19 UTC

    Grin:)

    @a=1.10; @b=1..100; @c=1..1000; @d=1..10000; use Benchmark 'cmpthese'; $ari = '@A = keys %{{ map +($_ => undef), @a, @b, @c, @d }};'; $buk = '@B = do{local %h; ++@h{@a,@b,@c,@d}; keys %h};'; cmpthese( -1, { ari=>$ari, buk=>$buk }); (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate ari buk ari 1.69/s -- -28% buk 2.36/s 39% -- cmpthese( -3, { ari=>$ari, buk=>$buk }); Rate ari buk ari 1.81/s -- -22% buk 2.32/s 28% -- cmpthese( -3, { ari=>$ari, buk=>$buk }); Rate ari buk ari 1.82/s -- -24% buk 2.40/s 32% -- print 'Same' if "@A" eq "@B" Same

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      You can speed up even more and make it strict safe by replacing local with my.

      Unfortunately in Perl 5.6.1 it seems that keys comes off in a different order for lexical and global hashes (with anonymous hashes coming off in the same order as global).

        Unfortunately in Perl 5.6.1 it seems that keys comes off in a different order for lexical and global hashes

        That's an interesting and unexpected observation. Do you have an example?

        Abigail