use strict; my $a = "This is a sentence"; my $b = "This is another sentecne"; my @matches = match_words($a,$b); print join(" ",@matches),"\n"; print scalar(@matches); sub match_words{ my @word_list1 = split(/\s+/,$_[0]); my @word_list2 = split(/\s+/,$_[1]); my $i = 0; while(defined($word_list1[$i]) && defined ($word_list2[$i]) && $word_list1[$i] eq $word_list2[$i]){ ++$i; } return splice @word_list1, 0, $i; } __END__ This is 2 #### my @result = splice @word_list1,0,$i; return @result;