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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.