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;