uniq#

Remove subsequent duplicates by string equality, preserving order.

Registered under both uniq and uniqstr. Both treat elements 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.

Synopsis#

my @subset = uniq    @values;
my @subset = uniqstr @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. undef compares equal to the empty string under uniqstr but is preserved as undef in the output by uniq.

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.