![]() |
|
Perl Monk, Perl Meditation | |
PerlMonks |
Re: Better way of doing!by kcott (Archbishop) |
on Aug 12, 2013 at 10:44 UTC ( #1049101=note: print w/replies, xml ) | Need Help?? |
G'day Xhings, That's not a good way to write your code at all. There's no correlation between $i, ..., $o and the elements in @patterns that each is associated with. You have to write separate code for every single one of those. It scales exceptionally poorly: what happens if need to add another keyword? What you really want is a hash whose keys are the keywords and values are the count. There's other problems as well in the while loop: you only count the first occurrence of the first keyword in each line; the smartmatch operator (~~) is experimental and unsuitable for production code. Here's some skeleton code you can use as a basis for rewriting your script:
See perlre if you're unfamiliar with named capture groups, i.e. (?<name>pattern). Update: Reviewing this code on the following day, I think named capture groups was probably overkill in this situation. Here's a simpler version using ordinary captures:
-- Ken
In Section
Seekers of Perl Wisdom
|
|