Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Merging two array(refs)

by LanX (Canon)
on Jan 21, 2013 at 07:47 UTC ( #1014406=note: print w/ replies, xml ) Need Help??


in reply to Merging two array(refs)

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


  • Comment on Re: Merging two array(refs)
    Select or Download Code

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://1014406]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2014-08-31 02:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (294 votes), past polls