uniq#

Remove subsequent duplicates by string equality, preserving order.

Elements are compared as strings: two values are the same when their stringifications are byte-for-byte identical. The first occurrence of any given value wins; later duplicates are dropped. undef is the one exception: it is distinct from the empty string, raises no warning, and is kept as undef in the result. That is the only difference from uniqstr.

Synopsis#

my @subset = uniq @values;
my $count  = uniq @values;    # scalar context - count only

What you get back#

In list context, the unique elements in first-seen order. In scalar context, the count of unique elements. uniq undef, '' returns both elements; later undefs are duplicates of the first.

Examples#

my @u = uniq    qw( a b a c b );       # ('a', 'b', 'c')
my @u = uniqstr 'a', 'A';              # ('a', 'A') - case matters
my $n = uniq    qw( a b a c b );       # 3

Differences from upstream#

Fully compatible with upstream.

See also#

  • uniqnum - compare as numbers rather than as strings.

  • uniqint - compare as integers after truncation.