allison has asked for the wisdom of the Perl Monks concerning the following question:

question how to I count the occurrences of word 'the' in a file? and how to I extract the numeric values in a () sample code:? $extract =~/^\( (\d.+) \)$/
  • Comment on count the occurrences of particular word in a file and extract number in ()
  • Download Code

Replies are listed 'Best First'.
Re: count the occurrences of particular word in a file and extract number in ()
by BrowserUk (Patriarch) on Mar 20, 2011 at 05:09 UTC

    1. how to I count the occurrences of word 'the' in a file?
      open my $fh, '<', 'theFile' or die $!; my $count = 0; while( <$fh> ) { ++$count while m[the]g; } print "'the' appeared $count times in 'theFile'";
    2. how to I extract the numeric values in a () sample code:? $extract =~/^\( (\d.+) \)$/
      if( $extract =~/^\( (\d.+) \)$/ ) { print "The number was $1"; } else { print "The regex didn't match"; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Um -- (1) would be an infinite loop, without a "g" in the regex. Use:
      use strict; my $count = 0; while( <DATA> ) { print; ++$count while m[the]ig; } print "'the' appeared $count times in 'theFile'\n"; __DATA__ THis is the dawning of the age of Aquarius. Good morning Starshine, the earth says hello. The end.
      For #2, simpler code:
      my ($extracted_value) = $extract =~/^\( (\d.+) \)$/; # Will be undef if nothing found.

           Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

        (1) would be an infinite loop, without a "g" in the regex

        Indeed, now corrected. I explicitly remove the /i (and accidentally the /g), because it would give wrong results if the OP really mean 'the' and not 'The' or 'THE'.

        simpler code: Will be undef if nothing found.

        That is no simpler as you still have to handle the possibility of undef.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: count the occurrences of particular word in a file and extract number in ()
by wind (Priest) on Mar 20, 2011 at 16:14 UTC
    how to I count the occurrences of word 'the' in a file?

    Per perlfaq4 and with the basic regex \bthe\b to indicate a word boundary as documented in perlre, we get:

    my $data = do {local $/; <DATA>}; my $count = () = $data =~ m/\bthe\b/ig; print $count; __DATA__ Then I played with the rabbit and the thesaurus. (2) It was quite the experience. (1) Seriously, the bunny was the coolest pet in the whole zoo. (3)
    - Miller