I tried figuring this out and have absolutely no idea. I looked at "Mastering Algorithms with Perl" but quickly got lost in the "Searching" chapter.

There are two data structures that are returned from DBI->fetchall_arrayref

I want to compare the arrays in both structures against each other and match on a specific element in the array, in this case the third element. All the arrays that match I want to store into a new data structure

Here is as far as I got, I need help with the subroutine:

#!/usr/bin/perl -w use strict; use Data::Dumper; # Array Reference my @array1 = ( ["a", "b", "match"], ["c", "d", "e"], ["f", "g", "h"], ["i", "j", "k"], ); my @array2 = ( ["l", "m", "z"], ["n", "o", "p"], ["q", "r", "s"], ["t", "u", "match"], ); # Make data structure similar to DBI's ->fetchall_arrayref; my $array_ref1 = \@array1; my $array_ref2 = \@array2; # Pass in data structures and define # which element to match against. my @matchedLists = &commonLists( \$array_ref1, \$array_ref2, 2 ); print Dumper \@matchedLists; sub commonLists { my ( $array_ref1, $array_ref2, $elementToMatch ) = @_; # Find the common lists where element $elementToMatch matches. # Result should return both lists that have "match" # in their third element. return \@match; }

In reply to 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.