Home: Perl Programming Help: Advanced:
extracting unique elements of array



lunus
Deleted

Oct 31, 2000, 7:11 AM


Views: 5418
extracting unique elements of array

Hi,

Does anyone know the elegant way to extract unique elements of array and how many times they appear in the original array? Actually I am through with the first part, but on second, I'm kind of lost.

Thanks,
Ruslana


japhy
Enthusiast / Moderator

Oct 31, 2000, 7:19 AM


Views: 5418
Re: extracting unique elements of array

This is a FAQ. Please read perldoc -q unique. Specifically, you want "How can I extract just the unique elements of an array?" from Perl FAQ 4. This is also available online at http://www.perldoc.com/ if you don't have the docs installed on your computer (although you really should).

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



sleuth
Enthusiast

Oct 31, 2000, 12:46 PM


Views: 5418
Re: extracting unique elements of array

 
Sheesh Japhy, You know the perl doc like the back of your hand :)

I couldn't even find it there so I would like to add to that. I think this code would be easy to use. Japhy, your the one who taught me it too.

undef %seen;
@unique=grep(!$seen{$_}++, @old);

And if you want to do specific field duplicate removal, then search forum posts, you'll find it, Japhy wrote out a whole block of code for it and It's really a good lesson.

Sleuth


japhy
Enthusiast / Moderator

Oct 31, 2000, 12:57 PM


Views: 5418
Re: extracting unique elements of array

The reason you couldn't find it in the 5.6 docs is because they CHANGED THE ENTRY (Jeff says "grrrr" to that). It's now listed as "How can I remove duplicate elements from a list or array?". It's still in perlfaq4, though.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



sleuth
Enthusiast

Oct 31, 2000, 5:58 PM


Views: 5418
Re: extracting unique elements of array

 
Oh yea, there it all is, cool. Well at least I remembered the code right, you taught me well, he he

Sleuth


sleuth
Enthusiast

Oct 31, 2000, 5:59 PM


Views: 5418
Re: extracting unique elements of array

 
OOps, forgot to at least let lunus know it's exactly on this page
http://www.perl.com/pub/doc/manual/html/pod/perlfaq4.html#How_can_I_extract_just_the_uniqu

Sleuth


BillKSmith
Veteran

Mar 3, 2011, 2:40 PM


Views: 5068
Re: [lunus] extracting unique elements of array

Use the module List::MoreUtils.
Good Luck,
Bill