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

Uniq with List::MoreUtils

  • uniq
  • distinct
  • List::MoreUtils

There are several ways to implement this without using an external module, but why would we want to reinvent the wheel when there is already a good solution in the List::MoreUtils module?

The only problem, but we see it all over the programming world is that this function called "uniq" would return a list of distinct elements instead of the ones that were unique in the original list.

#!/usr/bin/env perl
use strict;
use warnings;
use List::MoreUtils qw(uniq);

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

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