Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

'uniq' array (See related posts)

A much more idiomatic and efficient version of the 'uniq array' code at 152 is:

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



Comments on this post

pasang_lhamu posts on Jun 04, 2005 at 05:51
There's nothing efficient in using an extra var.
my @uniq = keys %{{ map { $_ => 1 } @list }};
towelrod posts on Feb 02, 2007 at 22:04
The original version maintains order, and pasang_lhamu's solution does not.
towelrod posts on Feb 02, 2007 at 22:06
I should also point out that effeciency wise, they are about the same. pasang is just creating an anonymous hash, so I'm not sure what you in terms of speed or memory usage. Possibly its better because the anonymous hash goes away faster, but even the named hash will go away when the block closes.

You need to create an account or log in to post comments to this site.


Click here to browse all 4862 code snippets

Related Posts