in reply to mashing two arrays

Even though your hash attempts have failed, I still think it's probably the way to go. It might look something like the following. There may be better ways to do it, but this seems simplest to me.
my %h = (); $h{$_->[0]} = [$->[1]] for @a1; # this will make empty arrayrefs automatically where necessary $h{$_->[0]}[1] = $_->[1] for @a2; my @b = map {[ $_ => @{$h{$_}} ]} sort keys %h;

(sort is probably ok to preserve the order here, but if it weren't, then Tie::IxHash might be helpful instead.)

-Paul

Replies are listed 'Best First'.
Re^2: mashing two arrays
by punkish (Priest) on Jan 19, 2007 at 17:51 UTC
    yes, thank you. On fixing a typo above, and one mod as below, it works (had to add the undef in the first assigment, else it doesn't show up wherever the second array elements don't exist).

    my %h = (); $h{$_->[0]} = [$_->[1], undef,] for @a1; $h{$_->[0]}[1] = $_->[1] for @a2; my @arr = map {[$_, @{$h{$_}}]} sort keys %h;
    --

    when small people start casting long shadows, it is time to go to bed

      Alternatives:

      my %h; $h{$_->[0]}[0] = $_->[1] for @a1; $h{$_->[0]}[1] = $_->[1] for @a2; my @arr = map [ $_, $h{$_}[0], $h{$_}[1] ], sort keys %h;
      # Nevermind, this is broken and not worth fixing. my %h1 = map @$_, @a1; my %h2 = map @$_, @a2; my @arr = map [ $_, $h1{$_}, $h2{$_} ], sort keys %h1, keys %h2;

      If you were dealing in hashes instead of arrays:

      my %hash = map { $_ => [ $h1{$_}, $h2{$_} ] } keys %h1, keys %h2;