in reply to Word frequency in an array
Hang on, I figured it out.
@fool = grep (/foo/, @array);
Are you sure?
What you have done there is extracted all the elements of @array that contain the string 'foo', and placed them in another array called @fool. If you want to know how many there are, you still need to do something with @fool. Of course, this is as simple as:
But there is no real need for the intermediate array, so you could have just done:my $count_foo = scalar @fool;
But wait, are you sure that is what you really want?my $count_foo = grep (/foo/, @array);
If the answer is only one, then you need to use something like this:my @array = qw(foo bar food foofighters kung-foo);
Cheers,my $count_foo = grep { $_ eq 'foo' } @array;
|
|---|