I don't understand if you want to count each occurrence of a single letter, that is: from:
@x = qw(a b c b c d c d e f)you want a report, something like:
a: 1; b: 2; c: 3; and so on, or you want a sub count which counts the recurrences of a single item you pass to it. That is: print count('c',@x) ; # prints 3
Since you already had solutions in the first case, I'll try to answer for the second. The reply is: grep
That is: count should just be something like this:
sub count { my ($string,@array) = @_ ; # count recurrence of $string in @array return scalar grep $_ eq $string, @array ; }
For more information on grep you can just do a perldoc -f grep
Ciao!
--bronto
In reply to Re: array counting
by bronto
in thread How can I count and display unique items in an array? (was: array counting)
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |