in reply to Re^2: Merging two array(refs)
in thread Merging two array(refs)

I've been staring at the screen for too long..

It's funny you say it doesn't merge, I've just copied the code from the post and used Data::Dumper to dump $arr at the end and I can see it merged as it should be..

No, the initial position of the member doesn't matter. If we look at an arbitrary first row of two arrefs:

$a->[0] = [qw/f0 f1 f2 f3/]; $b->[0] = [qw/e0 f2 e1 f0 e2];
If the value of the field in $b->[0] matches one already present in $a->[0] then all values in the matching field in the $b->1..$ arrays should be set in the already matched field. So the final array would be like:
$m = [ [qw/f0 f1 f2 f3 e0 e1 e2], [qw/a0 a1 a2 a3 "" "" ""/],# ..and so on all of $a [qw/b3 "" b1 "" b0 b2 b4/],# ..and so on for $b->[1..$]
a0,1,2.. being the first,second,third element of a $a->[x] array and likewise for the b0,1,2 of the $b->[x] array.

Does this make it any clearer?

Replies are listed 'Best First'.
Re^4: Merging two array(refs)
by choroba (Cardinal) on Jan 21, 2013 at 01:59 UTC
    Oh, wait, now I understand. The first member of the array is the header! I can see now what you mean by "merge", I imagined something different, which did not happen. I apologize for offending your code. Here is how I would do it. How sexy it is depends on the beholder's perspective:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $arr = [[qw/name pos loc age/], [qw/ike boss 12 44/], [qw/mat slave 22 21/], [qw/jill sec 15 32/],]; my $add = [[qw/car dog age/], [qw/a1 grr 3/], [qw/s2 miew 7/],]; my $i = 0; my %headers; for (@{ $arr->[0] }, @{ $add->[0] }) { $headers{$_} = $i unless exists $headers{$_}; $i++; } my $result = [ [ sort { $headers{$a} <=> $headers{$b} } keys %headers +] ]; my $new_columns = keys(%headers) - @{ $arr->[0] }; for (1 .. $#{$arr}) { push @$result, [ @{ $arr->[$_] }, (q()) x $new_columns ]; } my @rearranged = map $headers{$_}, @{ $add->[0] }; for my $row (1 .. $#{$add}) { my @new = (q()) x keys %headers; my $i = 0; $new[$_] = $add->[$row][$i++] for @rearranged; push $result, [@new]; } print Dumper $result;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks choroba, your code is definitely much clearer than mine. And yes, it is sexier =)