CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



lunus
Deleted

Oct 31, 2000, 7:11 AM

Post #1 of 7 (2725 views)
extracting unique elements of array Can't Post

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

Post #2 of 7 (2725 views)
Re: extracting unique elements of array [In reply to] Can't Post

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

Post #3 of 7 (2725 views)
Re: extracting unique elements of array [In reply to] Can't Post

 
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

Post #4 of 7 (2725 views)
Re: extracting unique elements of array [In reply to] Can't Post

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

Post #5 of 7 (2725 views)
Re: extracting unique elements of array [In reply to] Can't Post

 
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

Post #6 of 7 (2725 views)
Re: extracting unique elements of array [In reply to] Can't Post

 
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

Post #7 of 7 (2375 views)
Re: [lunus] extracting unique elements of array [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives