commit bf3ab33b3afde9fc29965b16431f1d5b5c232ace parent 0daf4a24cbdb1a84a87e2ab7ff87090cc50ee1fc Author: Samir Parikh <noreply@samirparikh.com> Date: Sun, 19 Dec 2021 16:55:59 +0000 add comments and cleanup code Diffstat:
M | word-count/WordCount.pm | | | 12 | ++++++------ |
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/word-count/WordCount.pm b/word-count/WordCount.pm @@ -7,12 +7,12 @@ our @EXPORT_OK = qw<count_words>; sub count_words { my ($sentence) = @_; my %word_count; - $sentence =~ s/(.)/\L$1/gi; - my @words = $sentence =~ m/([\w\d]+(?:'t)?)/gi; - foreach (@words) { - $word_count{$_}++; - #say $_; - } + $sentence =~ s/(.)/\L$1/gi; # force everything to lower case +# [\w\d]+ matches 1 or more "word" or decimal characters +# (?:'t)? matches optional apostrophe t +# the `?:` means a non capturing set of parentheses +# the `gi` at the end means global search and case insensitive + $word_count{$_}++ foreach ($sentence =~ m/([\w\d]+(?:'t)?)/gi); return \%word_count; }