#!/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; }