There are 2 errors in your code: a syntax error '$pattern = \b\w+\b;' and a logical one. Try this:
my ($count,$pattern,$file) = ( 0 , qr{\b\w+\b} , shift ); # declares and initializes the three variables # see perlop for qr// open FILE,$input or die "Error: $!"; # please read `perldoc -q 'quoting.*vars'` while(<FILE>){ $count += () = /$pattern/g; # that's kind of an idiom for counting the matches # it forces list context on m//g, but forces scalar # context on the resulting empty(?) list # that returns the number of elements that were # assigned to the list, although # in the end it's all thrown away, never reaching any # accessible memory outside the perl-internals... # -- it counts the number of matches } close FILE;
HTH
UPDATE: yes, qr// instead of qx//, i mixed 'em up, because i don't often use qr//. Thanks to sauoq and wog
--In reply to Re: Counting words
by fruiture
in thread Counting words
by raveguy2k
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |