OK, If I understand correctly you wanna merge the rows of two tables with partly intersecting columns?

You could use nested slices of position informations of the column-heads

  • to precalculate the positions hash just do something like

    DB<158> @head1= @{$arr1->[0]} => ("name", "pos", "loc", "age") DB<159> @head2= @{$arr2->[0]} => ("car", "dog", "age") DB<161> %pos=();$p=0 DB<162> for ( @head1 , @head2 ) { $pos{$_}=$p++ unless exists $pos{$_} } DB<163> %pos => ("dog", 5, "loc", 2, "car", 4, "name", 0, "pos", 1, "age", 3)

  • now preset the new rows with empty strings and do a list-assign to the calculated positions

    DB<168> @n= ("") x $p #default => ("", "", "", "", "", "") DB<169> @n[ @pos{@head1} ] = @{ $arr1->[1] } => ("ike", "boss", 12, 44) DB<170> @n => ("ike", "boss", 12, 44, "", "") DB<171> @n= ("") x $p #default => ("", "", "", "", "", "") DB<172> @n[ @pos{@head2} ] = @{ $arr2->[1] } => ("a1", "grr", 3) DB<173> @n => ("", "", "", 3, "a1", "grr")

    IMHO the sexiest approach so far! ;-)

    Completing the code is now just a minor task...

    HTH

    Cheers Rolf


    In reply to Re: Merging two array(refs) by LanX
    in thread 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.