in reply to Re: What's the best way to do a pattern search like this?
in thread What's the best way to do a pattern search like this?

supernewbie wanted an explanation of MeowChows sub:

First declare the sub

sub word_count {

Next we declare a lexically scoped has called %h the % indicates that this is a hash and the h is a typical MeowChow explanatory long var name :-)

my %h;

This is a bit of very idiomatic perl

$h{$_}++ for pop =~ /\w+/g;

It is fairly easy to understand if you read it R->L. The expression:

pop =~ /\w+/g

pop()s the last value off @_ which is the array passed to a subroutine called like mysub(@myarray). This gets us the value passed to the sub. We then use a regular expression to match \w+ which is groups of letters (as many in a row a possible) but not whitespace. Because this is referenced in LIST context by the for it returns a list of words which the for iterates over assigning each value to the magical $_ variable.

Finally we use out hash to count the occurances of each word (code). A hash stores a key value pair. Thus the key we are using is $_. The ++ part increments the value of $h{$_} by one each time we see the key.

%h

In a perl sub the sub returns the last value evaluated so this is shorhand for the more usual return %h

Hope this helps

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

  • Comment on Re: Re: What's the best way to do a pattern search like this?