Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Unique values

  • unique

You have a list of values with duplications, how can you create a unique list of the values?

#!/usr/bin/env perl
use strict;
use warnings;

my @data = qw(Earth Mars Earth Venus Earth Mars);
my @unique;
my %seen;

foreach my $value (@data) {
    if (! $seen{$value}) {
        push @unique, $value;
        $seen{$value}++;
    }
}

print "@unique\n"; # Earth Mars Venus