in reply to Counting occurances

I've been thinking 'bout this for a while now, and I cannot come up with anything better. I'm sure someone else can tho'. Anyway, if you just wanted to know the number of occuranes for one element, you could use:
@a=('a', 'b', 'a', 'a'); print "a occurs " . scalar grep(/a/,@a) . " times\n";

Replies are listed 'Best First'.
RE: RE: Counting occurances
by merlyn (Sage) on Jul 13, 2000 at 17:50 UTC
    Don't use a regex if you don't really want a regex, which you don't, because you now found all the items that contain "a", not just be equal to them. You probably wanted this instead:
    print "a occurs ".grep($_ eq "a", @list)," times\n";

    -- Randal L. Schwartz, Perl hacker

      OK, my point was to show that grep returned the number of occurances in scalar context, but ofcourse you're right. Point taken.