Is there a reason you don't use the database to join these two arrays? If you can't, you could use the following:

sub commonLists { my ( $array_ref1, $array_ref2, $elementToMatch ) = @_; my %lookup = map { $_->[$elementToMatch] => $_ } @$array_ref1; my @match; foreach my $fromArray2 (@$array_ref2) { my $fromArray1 = $lookup{$fromArray2->[$elementToMatch]}; push(@match, $fromArray1, $fromArray2) if $fromArray1; } return \@match; }

Or if the field is not a key:

sub commonLists { my ( $array_ref1, $array_ref2, $elementToMatch ) = @_; my %lookup; push(@{$lookup{$_->[$elementToMatch]}}, $_) foreach @$array_ref1; my @match; foreach my $fromArray2 (@$array_ref2) { my $fromArray1 = $lookup{$fromArray2->[$elementToMatch]}; push(@match, @$fromArray1, $fromArray2) if $fromArray1; } return \@match; }

But it won't work until you fix the bug in your code.
my @matchedLists = &commonLists( \$array_ref1, \$array_ref2, 2 );
should be
my @matchedLists = &commonLists( \@array1, \@array2, 2 );
or
my @matchedLists = &commonLists( $array_ref1, $array_ref2, 2 );

Your variable naming stayle is inconsistent. You use both fooBar and foo_bar for similar things. Be consistent.


In reply to Re: How to match specific elements of multiple array_references by ikegami
in thread How to match specific elements of multiple array_references by awohld

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.