in reply to Help on array element counting
Calling your array @fruits,
that works because grep in scalar context gives you the count.for my $fruit (qw{MANGO APPLES}) { print $fruit, ' => ', scalar( grep { $fruit eq $_ } @fruits), $/; }
The other and sometimes better way is to count them into a hash,
That is better when @fruits is not being updated and you need counts for many different items.my %fruit_count; $fruit_count{$_}++ for @fruits; for (qw{MANGO APPLES}) { print $_, ' => ', $fruit_count{$_}, $/; }
After Compline,
Zaxo
|
|---|