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

How do I count all the uniqueness of all the elements in an array and display the name onces and how many times the array element was counted.

Replies are listed 'Best First'.
Re: Elements in array
by rob_au (Abbot) on May 17, 2002 at 14:10 UTC
    You can solve this problem by making use of a hash, indexed by the array elements ...
    use Data::Dumper; my @array = ( 'a', 'b', 'c', 'a', 'b' ); # Iterate through array and increment hash element indexed # by the array element - As each hash element must be # unique, this allows for a count of each unique array # element to be easily generated. # my %hash; $hash{$_}++ for @array; print Data::Dumper::Dumper( \%hash );

     

      Good work! And without using Data::Dumper module output can be done via

      foreach (keys %hash) {print "$_ => $hash{$_}\n";}
Re: Elements in array
by mephit (Scribe) on May 17, 2002 at 19:47 UTC
    Here's a mappish WTDI:
    @array = qw/a b a c b d e c a/; %hash = map { $_ => ++$hash{$_} } @array; + printf "%s => %s\n", $_, $hash{$_} foreach sort keys %hash; --------- a => 3 b => 2 c => 2 d => 1 e => 1
    I had a hell of a time trying to figure this out, getting each total one less than it should have been, until I thought to use a prefix ++ instead of postfix. Code and learn, I guess. HTH.

    --

    Mephit (See my home node for my rant about Opera and PerlMonks, and my earliest nodes.)

Re: Elements in array
by meta4 (Monk) on May 17, 2002 at 19:13 UTC

    As usual with perl, TIMTOWTDI (There's More Than One Way To Do It).

    The following nodes list the methods above, and many more. Deciding unique elements of an array, How can I extract just the unique elements of an array?, and Return a Unique Array. Pick whichever method best solves the problem at hand.

    I found all the above nodes using the search box. I simply used most of the words in the title of your node, plus the key word in your question "unique". My search was simply "unique elements in array". This returned about 50 nodelets. The three above are just three I selected semi-randomly.

    Next time, try a search using the important words from your question and see if your question has already been answered. That way you get your answer right away.

Re: Elements in array
by DigitalKitty (Parson) on May 17, 2002 at 19:51 UTC
    Hi.

    Here is yet another way to accomplish the same task:
    #!/usr/bin/perl -w use strict; my %hash; my @info = qw( Katie Bob Aimee Carol John Carol ); foreach ( @info ) { $hash{$_}++; } print "\n"; foreach ( keys %hash ) { print "$_ -> ", $hash{$_}, "\n"; }


    Update: I 'fixed' the code in order provide an alternative solution.

    -DigitalKitty
Re: Elements in array
by kappa (Chaplain) on May 17, 2002 at 15:41 UTC
    Here's another way:
    my @array = qw(a b c d a a a a e); my %hash; @hash{@array} = 1 x @array; print join(':', keys %hash);
    Update: this code has nothing to do with the question, as tadman noticed :))) Bad kappa, no cookie and of course no more beer ;)
      To meet my sarcasm quota for the month, I am compelled to say:

      Where is the part where you count how many 'a's there are?