Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

unique elements in a Hash of Arrays?

by Anonymous Monk
on Jul 13, 2014 at 18:37 UTC ( [id://1093460]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
can you please guide me on how to find the number of unique elements in a hash of arrays?
I know that, if you have a normal array, you can just do:
$unique_ids = scalar(grep {defined $_} @normal_array);

but how can I do the same thing if there is a has of arrays involved? For example, how do you find the number of unique elements in the array for each of the keys in this example
%HoA = ( flintstones => [ "fred", "barney","barney", "nick", "john", "barne +y" ], jetsons => [ "george", "jane", "elroy", "mike", "elias" ], simpsons => [ "homer", "marge", "bart", "jack", "homer" ], );

Replies are listed 'Best First'.
Re: unique elements in a Hash of Arrays?
by AppleFritter (Vicar) on Jul 13, 2014 at 18:48 UTC

    Actually, this:

    $unique_ids = scalar(grep {defined $_} @normal_array);

    will not find the number of unique IDs in @normal_array, it'll simply count how many elements of @normal_array are defined. I recommend using the uniq function from List::MoreUtils.

    Applying it to a hash of arrays is straightforward: just iterate through the keys, and apply it to each element of your hash (i.e., each array). Using your example:

    foreach (sort keys %HoA) { say "$_: ", join ",", uniq @{ $HoA{$_} }; }

    Output:

    flintstones: fred,barney,nick,john jetsons: george,jane,elroy,mike,elias simpsons: homer,marge,bart,jack

    If you can't or don't want to use this module, the Perl Cookbook has several solutions (Recipe 4.6, p. 102).

      I recommend using the uniq function from List::MoreUtils. ... If you can't or don't want to use this module ...

      Anonymonk: ... just take a look at the code in it to see how it works; i.e., Steal This Code!

      Great thanks,
      can you also tell me how I can count the unique elements per key of the Hash of Arrays?

        Sure. uniq returns a list of unique elements, and lists evaluate to their number of elements in scalar context, so if you replace the line inside the foreach with e.g. this:

        say "$_: ", scalar uniq @{ $HoA{$_} };

        Then you'll get the number of unique items. (You could equally well assign to a variable for further processing, of course, and if you assign to a scalar, the explicit scalar won't be necessary, though it also won't hurt and may serve to make it a bit clearer what's going on.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found