Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to find number of unique elemenst in array

by gasho (Beadle)
on Nov 27, 2008 at 19:33 UTC ( [id://726470]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I have an array [A B C B D B D] and I wold like to print something like:
There are 1 element(s) of A There are 3 element(s) of B There are 1 element(s) of C There are 2 element(s) of D
If you can help me I will appreciate it

Thanks

(: Life is short enjoy it :)

Replies are listed 'Best First'.
Re: How to find number of unique elemenst in array
by ikegami (Patriarch) on Nov 27, 2008 at 20:33 UTC
    my %counts; ++$counts{$_} for @array; print("There are $counts{$_} element(s) of $_\n") for sort keys %counts;
      Thanks a lot ikegami, very elegant and short solution :)
      (: Life is short enjoy it :)
Re: How to find number of unique elemenst in array
by Corion (Patriarch) on Nov 27, 2008 at 19:35 UTC

    This sounds a lot like homework to me. But there is one rule of thumb in Perl - whenever you think of "unique", think "hash". Using a hash, with the array elements as keys and the count as values will give you an easy solution to your problem. Now all you have to do is write the code.

Re: How to find number of unique elemenst in array
by ysth (Canon) on Nov 27, 2008 at 20:27 UTC
    use strict; use warnings; use List::Util "reduce"; use Lingua::EN::Inflect "inflect"; use Algorithm::Loops "MapCar"; my @array = qw/A B C B D B D/; print map { map ( inflect("There PL(is,$_) $_ PL(element,$_) of "), $_->[1] +), "$_->[0]\n" } MapCar { [@_] } map { [sort keys %$_], [@$_{sort keys %$_}] } reduce { $a->{$b||$b}++; $a } {}, @array ;
    Sometimes it amuses me to place arbitrary constraints on myself in answering a question. In this case, I decided I was limited to one statement and no (declared) variables other than the input.

    Update: replaced an ugly reduce with MapCar.
    Update: additional topicalization

Re: How to find number of unique elemenst in array
by oko1 (Deacon) on Nov 27, 2008 at 21:24 UTC

    The solution offered by ikegami is really the Right Thing here, but - just to be silly -

    #!/usr/bin/perl -wl use strict; my @a; $a[ord($_)-65]++ for <DATA>; print "There are $a[$_] element(s) of ", chr($_+65) for 0..$#a; __DATA__ A B C D A B A C A D

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: How to find number of unique elemenst in array
by GrandFather (Saint) on Nov 27, 2008 at 20:20 UTC

    Life is too short to spend it reinventing wheels - use CPAN. In this case the various List modules will likely have what you need. Enjoy.


    Perl's payment curve coincides with its learning curve.
      Counting repeated elements isn't exactly reinventing a wheel. What List modules/functions did you have in mind? (My List::Util::reduce solution was by way of a joke; I hadn't seen your post when I wrote it.)

        Oh ysth! Look at the OP's sig, then look again at my reply. ;)

        Although actually List::Uniq would seem to fit quite well.


        Perl's payment curve coincides with its learning curve.
Re: How to find number of unique elemenst in array
by ccn (Vicar) on Nov 27, 2008 at 20:51 UTC
    perldoc perlfaq4 #How can I remove duplicate elements from a list or array?
Re: How to find number of unique elemenst in array
by Skeeve (Parson) on Nov 27, 2008 at 22:33 UTC

    My constraint: Don't use a hash!

    my @array = qw/A B C B D B D/; my $c=1; foreach my $k (sort @array) { next if --$c; $c= scalar grep { $_ eq $k } @array; print "There are $c element(s) of $k\n"; }

    Update: Fixed thanks to gashos coment.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      It failed test with my @array = qw/history historyA historyB history historyA D/;
      (: Life is short enjoy it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726470]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found