On Jul 9, 2014, at 1:20 PM, Natxo Asenjo wrote:

> hi,
> 
> i have an array of arrays which contains equal elements. I would like to 
> isolate the unique values.

Do you mean that the subarrays contain equal NUMBERS of elements?

> 
> I have tried using the uniq method of  List::MoreUtils but it apparently does 
> not work with an AoA.

That is true. List::MoreUtils::uniq expects a list of scalars (actually, all 
lists are lists of scalars). The function cannot figure out that the scalars in 
the list you have passed to it are actually references to other lists.


> This is what I tried:
> 
> for my $i ( @$data_ref ) {
>     push $seen_ref, [ $i->{'value1'}, $i->{'value2'}, $i->{'value3'}, 
> $i->{'value4'}, ];
> }
> 
> my @unique = uniq $seen_ref;
> 
> But unfortunately it does not work.
> 
> How could I achieve this?



If you want to find out the unique values in any collection, insert those 
values as keys in a hash. Since keys in a hash are unique, when you are done 
inserting, the keys of the hash will be the unique values from your collection.

It doesn't matter what values you insert in the hash to go along with your 
keys. You are only interested in the keys. However, one common method would be 
to increment the hash value each time you insert a key. That way, when you are 
done, you will have a count of the number of times each value appears in your 
original collection:

  $hash{$key}++;

where $key takes on each of the values in your original collection.

Try to implement that scheme, and post your code here if you have any problems.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to