#!/usr/bin/perl -w use strict; use Data::Dumper; # While matching the hash, elem of each array must occur together # and in order my @ar1 = ('A','B'); #Answer: 3 my @ar2 = ('B','A'); #Answer: 1 my @ar3 = ('A','B','C'); #Answer: 2 my $hash = { 'S1' => [ 'A', 'B', 'C', 'A' ], 'S2' => [ 'A', 'C', 'D', 'B' ], 'S3' => [ 'C', 'A', 'D', 'H' ], 'S4' => [ 'A', 'B', 'I', 'C' ] }; count_($hash,\@ar1); count_($hash,\@ar2); sub count_ { my ($hashref,$arref) = @_; my $counter = 0; my $test = join('.*?',@$arref); print "Query: ",join('',@$arref),"\n"; for my $key(sort{$a cmp $b}keys(%$hashref)){ my $string = join('',@{$hashref->{$key}}); print $string," - "; if($string =~ $test){ print "1\n"; $counter++; } else{ print "0\n"; } } print "Total counter: ",$counter,"\n" }# end count_