Hi wise ones,

I'd like to merge two arrayrefs into one containing all the fields but not repeating them. For example:

my $arr1 = [[qw/name pos loc age/], [qw/ike boss 12 44/], [qw/mat slave 22 21/]]; my $arr2 = [[qw/car dog age/], [qw/a1 grr 3/]]; #...magic goes here and we want: my $arrMighty = [[name, pos, loc, age, car, dog], [ike, boss, 12, 44, "", ""], [mat, slave, 22, 21, "", ""], ["", "", "", 3, a1, grr]];

I don't need to have it in any order as long as the values in the original arrays match their new position (or empty string). Oh yes, the first arefs ($arr1->[0] and $arr2->[0]) are like field names and they merge into one row, the others are data..

I've gone counting the fields and adding them and going trough the arrays over again, it works but it looks like a mess and I'm just wondering if there is some sexy way to do this..

Much obliged for any enlightenments..

In reply to Merging two array(refs) by kpiti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.