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

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

And then there is always CPAN ...

// ...for those who want to keep it simple

use Uniq;
my @u_s_users=uniq sort @users;

'uniq' array

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;


Efficient 'uniq' snippet

This snippet will quickly uniq an unsorted array; in other words, remove duplicates. (note: for large arrays, it may be more efficient to sort the array first and simply keep track of consecutive dups instead of using a hash.)

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


« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS