Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  LIST::UNIQ (3)

.ds Aq ’


List::Uniq - extract the unique elements of a list



  use List::Uniq :all;

  @uniq = uniq(@list);

  $list = [ qw|foo bar baz foo| ];
  $uniq = uniq($list);


List::Uniq extracts the unique elements of a list. This is a commonly re-written (or at least re-looked-up) idiom in Perl programs.


    uniq( { OPTIONS }, ele1, ele2, ..., eleN )

uniq() takes a list of elements and returns the unique elements of the list. Each element may be a scalar value or a reference to a list.

If the first element is a hash reference it is taken to be a set of options that alter the way in which the unique filter is applied. The keys of the option set are:
o sort

If set to a true value, the unique elements of the list will be returned sorted. Perl’s default sort will be used unless the <B>compareB> option is also passed.

<B>sortB> defaults to false.

o flatten

If set to a true value, array references in the list will be recursively flattened, such that

  ( foo, [ [ bar ] ], [ [ [ baz, quux ] ] ] )


  ( foo, bar, baz, quux )

<B>flattenB> defaults to true.

o compare

A code reference that will be used to sort the elements of the list if the <B>sortB> option is set. Passing a non-coderef will cause <B>uniqB> to throw an exception.

The code ref will be passed a pair of list elements to be compared and should return the same values as the cmp operator.

Using a custom sort slows things down because the sort routine will be outside of the List::Uniq package. This requires that the pairs to be compared be passed as parameters to the sort routine, not set as package globals (see sort in perlfunc). If speed is a concern, you are better off sorting the return of <B>uniqB> yourself.

The return value is a list of the unique elements if called in list context or a reference to a list of unique elements if called in scalar context.



Nothing by default.

Optionally the <B>uniqB> function.

Everything with the <B>:allB> tag.


If you want to unique a list as you insert into it, see Array::Unique by Gabor Szabo.

This module was written out of a need to unique an array that was auto-vivified and thus not easily tied to Array::Unique.


James FitzGibbon <>


The idioms used to unique lists are taken from recipe 4.7 in the Perl Cookbook, 2e., published by O’Reilly and Associates and from the Perl FAQ section 5.4.

I pretty much just glued it together in a way that I find easy to use. Hopefully you do too.


Copyright (c) 2004-2008 Primus Telecommunications Canada Inc.

Copyright (c) 2008-2010 James FitzGibbon

All Rights Reserved.

This library is free software; you may use it under the same terms as perl itself.

Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 LIST::UNIQ (3) 2010-06-18

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.