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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.