in reply to How do I just count any words from a file?

The regular expression isn't an array you can foreach through. It returns something like a true false value while the iterator is true instead.

So choose while instead of foreach here. See if something like this works better:

$wordcount ++ while $buffer =~ m/\b\w+\b/g

-Paul

Replies are listed 'Best First'.
Re^2: How do I just count any words from a file?
by Cazbo (Initiate) on Jun 04, 2007 at 12:51 UTC
    In reply to both of you, thank you very very very much :) Basically it's a project connected with my course. I had to create a simple .txt file containing the verse "Hey diddle diddle...", request input of and test the filename format, test the files existence and size, then count the characters, words, paragraphs etc in it, then print them to <STDOUT>. I've managed the characters and lines parts, but hit a wall of mental block when it came to the words etc. Anyway, that part now works, and I can move onto the paragraphs and and sentences to complete it. Although I've come to the end of learning Perl through my course, I'm a bit of a stubborn wench and refuse to let it go. I struggled with the implementation of it, despite getting high marks for the theory, and I'm not satisfied with my performance to date. Therefore, I'd like very much to stay with Perl Monks and use the exercises/examples here to further develop in this field. Respect *bows* Caz :)
      Spend as much time on learning regular expressions as is necessary to really understand and enjoy them. You won't regret it. All the modern high level languages (and even some low level ones) seem to support them now, so it's worth spending the effort on it no matter whether it's for a class or not.

      -Paul

Re^2: How do I just count any words from a file?
by bart (Canon) on Jun 04, 2007 at 14:37 UTC
    The regular expression isn't an array you can foreach through
    It returns a list of matches. So actually, he can.
    It returns something like a true false value while the iterator is true instead.
    Not with /g in list context. Even when there are no capturing parens, because then you'll still get a list of matches for the entire pattern.
    So choose while instead of foreach here. See if something like this works better
    It'll use less memory, but otherwise, both should have pretty much the same end result.
Re^2: How do I just count any words from a file?
by varian (Chaplain) on Jun 04, 2007 at 14:45 UTC
    The regular expression isn't an array you can foreach through. It returns something like a true false value while the iterator is true instead.
    Not true, the OP used the 'g' flag which makes the regexp return a list of matched groupings when in list context (and foreach expects a list).

    So to combine foreach with a regexp is just fine, in fact Cazbo you are close to the solution!

    Hint: to debug execute a

    print "$wordcount, $_\n";
    within your foreach loop immediately after you do the autoincrement and see what it prints. Your regexp needs some finetuning and what about the counter itself?
Re^2: How do I just count any words from a file?
by princepawn (Parson) on Jun 04, 2007 at 13:25 UTC
    I suspected your \b was un-necessary and the following test file implies that it does...
    use strict; my $line = 'hi there my name is bob'; sub m1 { my $line = shift; my $re = '(\w+)' ; my @match = ( $line =~ /$re/g ) ; return scalar @match ; } sub m2 { my $line = shift; my $re = '\b\w+\b' ; my $wordcount; $wordcount++ while $line =~ /$re/g ; return $wordcount ; } sub m3 { my $line = shift; my $re = '\w+' ; my $wordcount; $wordcount++ while $line =~ /$re/g ; return $wordcount ; } print m1($line); print m2($line); print m3($line);


    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality