Perl Tip Of The Day: “uniq” an Array

I can across a great snippet of code, posted on DZone, that provides a two line solution to uniq an array while maintaining order:

# Input: @list
# Output: @uniqed
 
my %seen;
my @uniqed = grep !$seen{$_}++, @list;

#From TMtm on DZone.com

This entry was posted in Software Development and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">