Too Cool for Internet Explorer


Swobodin's Profile | Swobodin's Blog | Back to Fedora Tunisia

Perl uniq array, to remove duplications

With Bash, you can simply remove duplications by typing:
sort file | uniq
With Perl, it’s more funny. I found those pieces of code in Code Snippets.
Simple ‘Uniq’ code

my @uniq = keys %{{ map { $_ => 1 } @list }};

 
Another one, more efficient

my %u = ();
@uniqed = grep {defined} map {
if (exists $u{$_}) { undef; } else { $u{$_}=undef;$_; }
} @list;
undef %u;

Related posts

(sometimes, the plugin foolishes)
Simple Perl loop script
Snipper
Perl Poetry
GraphInside
Search’n'Replace

Technorati tags

5 Responses to “Perl uniq array, to remove duplications”

  1. etc Says:

    ou les deux, exec “sort $file | uniq”;

  2. Swobodin Says:

    exec is for lamers :-P

  3. etc Says:

    run u prog as faster as u can!
    et puis pkoi reinventer la roue swobo. :p

  4. samsoum Says:

    Hmm, I played with Perl in a previous life, so I hope I am not embarassing myself by commenting here :-)
    First, it seems that you’re making the assumption that you can change the sorting order of the input array thus the use of the map. But sometimes you need to preserve the original order and the proposed soultions won’t work. Second, the map is ok up to small to medium sized array, but it won’t scale for big arrays and in this case there is nothing better than a n old classic loop, sometimes simple is better :-)
    Nice blog you have here, I saw your profile and I am very impressed and also proud of your achievements, i am myself a geek but an old one (I am 40). Keep it up, thks.

  5. Swobodin Says:

    Welcome Samsoum :-)
    Cool to find and old generation hacker: We can learn from your experience!
    In fact, this algorithm doesn’t keep the array elements order; it sorts them instead.
    Have fun!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

 
 

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

You must read and type the 5 chars within 0..9 and A..F, and submit the form.

  

Oh no, I cannot read this. Please, generate a


Top Tunisie Blogs