#! /usr/bin/perl use strict; use warnings; my $str1='It is a guide to action which ensures that the military always obey the commands of the party.'; chomp($str1); my $str2='It is a guide to action that ensures that the military will forever heed Party commands is a guide.'; chomp($str2); my @arr1=split(/\s+/, $str1); my $n_occur_in_str1=0; my $n_occur_in_str2=0; print "-"x60,"\n"; print "nth Match\tNo of occurence\t\tMatched Pattern\n"; print "-"x60,"\n"; for(my $i=0; $i<$#arr1;$i++) { my $t1="$arr1[$i] $arr1[$i+1] $arr1[$i+2]"; my @pattern_word_count=split(/\s/, $t1); my $space_count=@pattern_word_count; if($space_count == 3) { if($str2=~/$t1/) { my $occurence=`echo $str2 | grep -o '$t1' |wc -l`; chomp($occurence); print $i+1,"\t\t",$occurence,"\t\t\t$t1\n"; $n_occur_in_str2+=$occurence; $n_occur_in_str1++; } } } print "\n\nTotal no of occurence in str1 : $n_occur_in_str1\n"; print "\n\nTotal no of occurence in str2 : $n_occur_in_str2\n";