Hi Monks!

How could I combine or merge these two arrays based on if $ar_ref_1->[1] AND $ar_ref_2->[0] match, thanks or the help!

use strict; use warnings; use Data::Dumper; my $ar_ref_1 = [ [ 'USER 1', '12345', '2009', 'X', '0', '8.00', ' ', '1- E' ], [ 'USER 3', '1234567', '2009', 'X', '0', '8.00', ' ', '3- E' ], [ 'USER 2', '123456', '2009', 'X', '0', '8.00', ' ', '2- E' ], [ 'USER 5', '123456789', '2009', 'X', '0', '8.00', ' ', '5- E' ], [ 'USER 4', '12345678', '2009', 'X', '0', '8.00', ' ', '4- E' ], ]; my $ar_ref_2 = [ [ '1234567', '12', '0', '2', '0', '0', '0', '0', '0', '0', '0', '0' ], [ '123456789', '0998', '2', '2', '3', '0', '0', '0', '0', '0', '0', '0' ], [ '123456', '657', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' ], [ '12345678', '8754', '0', '2', '0', '0', '0', '0', '0', '0', '0', '0' ], [ '12345', '876', '1', '2', '3', '0', '0', '0', '0', '0', '0', '0' ], ]; for my $i (0..$#$ar_ref_1) { for my $j (0..$#$ar_ref_2) { if($ar_ref_1->[1] == $ar_ref_2->[0]){ push @{$ar_ref_1->[$i]} +, @{ ref($ar_ref_2->[$i]) eq 'ARRAY' ? + $ar_ref_2->[$i] : [('-') x 3] }; } } } print Dumper $ar_ref_1;

Here is what I am trying to get, I am sampling one element as an example:
[ 'USER 4', '12345678', '2009', 'X', '0', '8.00', ' ', '4- E' '12345678', # I need to exclude the duplicated element out + as well, it’s a nightmare! '8754', '0', '2', '0', '0', '0', '0', '0', '0', '0', '0' ],

Thank you!

In reply to Compare and merge array elements help! by Anonymous Monk

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.