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).
In reply to Re: unique elements in a Hash of Arrays?
by AppleFritter
in thread unique elements in a Hash of Arrays?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |