http://qs1969.pair.com?node_id=928398

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

Hi Monks....again...

OK, so I've got an @ with several hundred elements, some of them "batch", some initials, some numbers, all different lengths. I need to extract only the unique elements, and discard double elements.

I discussed a bit ago in CB, was told to use cmp, I'm not understanding how to get the elements of the array, to iterate through $a and $b in a function, all several hundred of them.

So I went to the cookbook, to find a way to count them with a hash. This is what I found:

#!usr/bin/perl use strict; #use warnings; my %seen = ( ); my @uniq = ( ); my @list = "bob, bob, sue, sue"; my $item; my $element; foreach $item (@list) { if ($seen{$item}) { # same, don't grab } else{ # if we get here, we have not seen it before $seen{$item} = 1; push(@uniq, $item); } } foreach $element(@uniq) { print $element . "\n"; }

But it doesn't work. And I haven't even tried adding numbers and such in with the test array yet. Can someone please break this down for me like I'm a two year old...I have never understood the cmp routines in examples, because I don't get how to pass all the hundreds of elements in my @ into to $a and $b to be compared. I understand how it compares after that, just not that first part. Please help me, I'm starting to see green Matrix code running down my eyes....

Thanks koolgirl

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw