in reply to Word frequency in an array

What is the best way to count how many times the word "foo" occurs in @array?
If you only want to count occurances, use grep in scalar context

my $count_foo = grep (/foo/, @array);

Replies are listed 'Best First'.
Re^2: Word frequency in an array
by McDarren (Abbot) on Jun 10, 2007 at 16:33 UTC
    Lets use that in an example...
    my @array = qw(foo bar food foofighters kung-foo); my $count_foo = grep (/foo/, @array); print "$count_foo\n";
    Prints "4" - which may or may not be what the OP was after (I suspect not).
    my $count_foo = grep { $_ eq 'foo' } @array;
    ..might be more what the OP was looking for.

    Cheers,
    Darren :)