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

Okay, assume that I have this array generated from a database query:
@status = ("Applied", "Enrolled", "Admitted", "Applied", "Enrolled");
I'm trying to figure out the most elegant way on how to provide summary statistics on the items on that array with perl. The output would be some kind of associative array:
@statuscount = ("Admitted" => "1", "Applied" => "2", "Enrolled" => "2" +);
I know that I can do this by doing another database call using COUNT, but I want to minimize the number of database queries that I do. Thoughts anyone?
TIA

Replies are listed 'Best First'.
Re: Counting elements in array and outputing the count and item
by toolic (Bishop) on Jun 17, 2008 at 18:25 UTC
    Use a hash:
    use strict; use warnings; use Data::Dumper; my @status = ("Applied", "Enrolled", "Admitted", "Applied", "Enrolled" +); my %statuscount; $statuscount{$_}++ for @status; print Dumper(\%statuscount); __END__ $VAR1 = { 'Enrolled' => 2, 'Applied' => 2, 'Admitted' => 1 };
Re: Counting elements in array and outputing the count and item
by Corion (Patriarch) on Jun 17, 2008 at 18:28 UTC

    The most elegant way is still to do this in the database:

    select item, count(*) from mytable group by item

    ... but you can do the same in Perl by using a hash:

    use strict; use Data::Dumper; my %count; my @status = ("Applied", "Enrolled", "Admitted", "Applied", "Enrolled" +);; $count{$_}++ for @status; warn Dumper \%count;
Re: Counting elements in array and outputing the count and item
by monarch (Priest) on Jun 17, 2008 at 18:28 UTC

    Tried and tested:

    use strict; use Data::Dumper; # original data my @status = ( "Applied", "Enrolled", "Admitted", "Applied", "Enrolled" ); # count keys my %tmphash = (); $tmphash{$_}++ for ( @status ); # convert into an array, as requested my @statuscount = %tmphash; # display print( Data::Dumper::Dumper( \@statuscount ) );

    Outputs:

      Works like a charm. Thanks. To the previous comment about not doing it in SQL; The query is rather complex and returns a lot of other data I use for processing; Another db query I think slow things down. Thanks for you help! I knew there was a more elegant way of doing this!
        Another db query I think slow things down.
        My (inexpert) rule of thumb is that if you've gone to the trouble of getting a connection and have run one or more queries then running some more is unlikely to slow things down. The data you need may already be in the db's cache in which case it will be a lot quicker than getting Perl to do it.

        Let the db take the strain. As Dolly Parton said, "It's what they're for!" although she was talking about something else. :-)

Re: Counting elements in array and outputing the count and item
by psini (Deacon) on Jun 17, 2008 at 18:26 UTC

    Are you sure that you can't do it with the same database query?

    What is the query you do?

    Careful with that hash Eugene.

Re: Counting elements in array and outputing the count and item
by psini (Deacon) on Jun 17, 2008 at 18:30 UTC

    Sorry, I saved only half of the answer.

    To do it in Perl I would try:

    my %statuscount; foreach my $stat (@status) { $statuscount{$status}++; }

    Careful with that hash Eugene.