in reply to Re: mashing two arrays
in thread mashing two arrays

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

Replies are listed 'Best First'.
Re^3: mashing two arrays
by ikegami (Patriarch) on Jan 19, 2007 at 18:06 UTC

    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;