koolgirl has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Extracting unique elements from array
by toolic (Bishop) on Sep 28, 2011 at 19:26 UTC | |
by moritz (Cardinal) on Sep 28, 2011 at 19:31 UTC | |
by koolgirl (Hermit) on Sep 28, 2011 at 19:34 UTC | |
by wink (Scribe) on Sep 28, 2011 at 20:00 UTC | |
by Marshall (Canon) on Sep 28, 2011 at 22:14 UTC | |
by Anonymous Monk on Sep 28, 2011 at 21:17 UTC | |
Re: Extracting unique elements from array
by aaron_baugher (Curate) on Sep 28, 2011 at 21:13 UTC | |
Re: Extracting unique elements from array
by umasuresh (Hermit) on Sep 29, 2011 at 17:36 UTC | |
by SimonSaysCake (Beadle) on Dec 12, 2015 at 14:26 UTC |