Task: in the input stream a number is given - the number of sentences containing words and, possibly, some sort of separating garbage; after the number on each line are the sentences themselves; followed by a number - the number of keywords; after it on each line are the words themselves.
You need to find the number of occurrences of each keyword in each sentence.
Definition of the word:
Or the word under (the ascii value 95).
We define a substring as follows.
It is a part of a word. The given substring must be preceded by letters or numerics or an underscore. There is no need for a word on the other side of the line.
Can I improve my code? Make it shorter and more efficient in the number of operations. Perhaps, with the help of functional programming, you can cut it all in half? Pearl has this.
#!/usr/bin/perl use strict; chomp(my $lines_count = <>); # read the number of centences my @words; for (1..$lines_count) { my $line = <>; push @words, split /[^\w]/, $line; } my $pattern_count = <>; for (1..$pattern_count) { chomp(my $input = <>); my $result = 0; foreach (@words) { if (/\w+$input\w+/) { $result++; } } print $result, "\n"; } Sample Input
1 existing pessimist optimist this is 1 is
Sample output
3
Explanation
'existing' has 'is' as a substring and is both preceded and succeeded by words as defined above.
'pessimist' has 'is' as a substring for the same argument as above.
'optimist' has 'is' as a substring for the same argument as above.
'this' though has 'is' as a substring and it has been followed by a blank space, which is non-letter and non-underscore
'is' is not included as it is preceded and succeeded by a [blank space] which is non-letter, non-numeric and non-underscore.
+to/\w+$inputabsolutely useless; you just need to make sure that before and after $ input is \ w - Mike