in reply to Number of matching words
#! perl -slw use strict; sub nWordsMatch { my( $str1, $str2 ) = @_; my $p = 0; $p++ while substr( $str1, $p, 1 ) eq substr( $str2, $p, 1 ); my @words = substr( $str1, 0, $p-1 ) =~ m[\b(\w+)\b\W+]g; return @words; } my @matchWords = nWordsMatch( 'This is a sentence', 'This is another sentence.' ); print "@matchWords"; my $count = @matchWords; print $count; @matchWords = nWordsMatch( 'The quick brown fox jumps over the lazy dog', 'The quick brown fox jumps over teh lazy dog', ); print "@matchWords"; print scalar @matchWords; __END__ c:\Perl\test>test2 This is 2 The quick brown fox jumps over 6
|
|---|