in reply to Number of matching words
updated: You need to use the match_words() in array context, if you use it in scalar context, it will return the last one matched (e.g., "is" in this example), this is due to the same behavior for splice. To solve this problem, you may want to replace the return statment in the sub byuse 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;
|
|---|