Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I count the number of times a specific item in an array appears? And display to the browser the item's name and the number it has appeared in the array? Thanks!!!

edited: Fri Jun 21 15:18:36 2002 by jeffa - changed title

  • Comment on How can I count and display unique items in an array? (was: array counting)

Replies are listed 'Best First'.
Re: array counting
by mephit (Scribe) on Jun 20, 2002 at 22:09 UTC
    @a = qw/foo bar foo baz bar qux foo/; $hash{$_}++ foreach @a;
    The counts are in %hash now. HTH.

    --

    There are 10 kinds of people -- those that understand binary, and those that don't.

Re: array counting
by sschneid (Deacon) on Jun 20, 2002 at 22:11 UTC
    There may be a more concise way, but...
    my $i = 0; my $a = 'match_this' foreach my $b (@array) { if ($a eq $b) { $i++ } } print "Total occorunces of $a: $i";
    Hope it helps.

    scott.
Re: array counting
by tjh (Curate) on Jun 21, 2002 at 01:25 UTC
    It's amazing how many responses came up in Super Search for the words "count unique array".

    You should try it to see other approaches...

Re: Counting displaying unique array items
by thelenm (Vicar) on Jun 20, 2002 at 22:22 UTC
    I'm not sure about displaying it to the browser, but you can count the number of array elements matching certain criteria with grep. For example, to count the number of times 'foo' appears in an array:
    $num_foos = grep {$_ eq 'foo'} @array;

    -- Mike

    --
    just,my${.02}

Re: array counting
by bronto (Priest) on Jun 21, 2002 at 08:06 UTC

    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

Re: Counting displaying unique array items
by meraxes (Friar) on Jun 20, 2002 at 22:46 UTC

    Provided it's not to memory intensive you can stick everything into a hash and iterate through the hash:

    foreach $item (@yourList) { $newHash{$item}++; } print "<TABLE>\n"; foreach $item (keys %newHash) { print "<TR><TD>$item</TD><TD>$newHash{$item}</TD></TR>\n"; } print "</TABLE>\n";

    This is, of course, a very simple answer and I'm sure there are more efficient ways to do it, but there you are.

    EDIT: Whoops, didn't realize this fella posted the same question 3 times (naughty). The original question is already answered quite sufficiently here by mephit in a much more compact way than my solution.

    |\/|eraxes

Re: How can I count and display unique items in an array?
by joshua (Pilgrim) on Jun 20, 2002 at 23:05 UTC
    This oughta work...
    use strict; my @array = qw(foo bar foobar foobar bar bar bar bar foo foo foo foo b +ar foobar); my %hash; $hash{$_}++ foreach @array; print "Content-type: text/plain\n\n"; foreach (keys %hash) { print "$_ appears $hash{$_} times\n"; }
    The above will return...
    foobar appears 3 times foo appears 5 times bar appears 6 times

    Joshua
Re: array counting
by marvell (Pilgrim) on Jun 21, 2002 at 08:53 UTC
    Something based on:
    @list = qw(bing spam bing sponge bob pub time wibble sponge sponge); $count{$_}++ for @list; print map {"$_\t$count{$_}\n"} sort keys %count;

    --
    Steve Marvell

      Nice fillwords. :)

      Makeshifts last the longest.

Re: How can I count and display unique items in an array?
by emilford (Friar) on Jun 20, 2002 at 22:58 UTC
    I believe your exact question was asked and answered in the previous post. Check the answers there and beware of the NodeReaper! :)

    # to repeat Meraxes foreach (@array) { $seen_hash{$_}++; } # then just do a print for each key in the %seen_hash hash print "Content-type: text/html\n\n"; # viewable in browser foreach (keys %seen_hash) { print $seen_hash{$_}; }
Re: How can I count and display unique items in an array?
by Aristotle (Chancellor) on Jun 20, 2002 at 22:59 UTC
    You build a hash with the elements as keys and an array reference as value. The referenced array contains the indices of the element indexed by the key, and the number of elements of that array is logically the number of occurences of the element in the original array.
    my (@array, %hash); push @{$hash{$array[$_]}}, $_ for 0..$#array; for(keys %hash) { print "Element: $_\nOccurences: ", scalar @{$hash{$_}}, "\n"; print("At indices: ", join(",", @{$hash{$_}}), "\n"); }
    ____________
    Makeshifts last the longest.