How to use List::MoreUtils uniq to remove duplicates in Perl?
Question
How to use List::MoreUtils uniq to remove duplicates in Perl?
Removing Duplicates from an Array Using List::MoreUtils::uniq
The List::MoreUtils module provides several useful utilities for list manipulation, including the uniq function which can be used to remove duplicate elements from an array. It offers a simplified and efficient way to get unique values while preserving the order of their first occurrence.
What is uniq in List::MoreUtils?
uniq takes a list and returns a new list containing only the first occurrence of each distinct element, effectively removing duplicates. Unlike uniq in some other modules, it preserves the original order of elements.
Basic Usage
- Import
uniqfromList::MoreUtils. - Pass your list (array) to
uniq. - Assign the returned list to a new array or overwrite the existing one.
This requires you have List::MoreUtils installed. It is a core module bundled with Perl since 5.10, so you likely have it already.
Example Code
use strict;
use warnings;
# Import uniq from List::MoreUtils (core module since Perl 5.10)
use List::MoreUtils qw(uniq);
# Example array with duplicates
my @data = qw(apple banana apple orange banana grape apple);
# Remove duplicates, preserving first occurrence order
my @unique = uniq @data;
print "Original list: @data\n";
print "Unique list: @unique\n";
Explanation
qw()is a convenient way to create a list of strings.uniq @dataprocesses the list and returns only unique values.- The result is stored in
@uniqueand printed out.
Output
When you run the above Perl script, you'll get:
Original list: apple banana apple orange banana grape apple
Unique list: apple banana orange grape
Perl Concepts Demonstrated
- Sigils:
@sigil denotes arrays (@data,@unique). - Context:
uniqexpects a list, returns a list, so scalar vs. list context matters. - Modules and Importing: You import
uniqexplicitly to use it. This is common CPAN practice.
Common Pitfalls
- Make sure to import
uniqproperly or you’ll get an error. List::MoreUtils::uniqremoves duplicates based on string equality, not deeper data structures or references.- Preserved order is based on first occurrence only — last occurrences are discarded.
- Don’t confuse
List::MoreUtils::uniqwithList::Util::uniq, which appeared later with different behavior in Perl 5.36+.
Summary
Using List::MoreUtils::uniq is a clean, simple way to remove duplicates from an array in Perl while preserving order. It’s widely available on modern Perl installations and is perfect for beginners learning list manipulation.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 59ms
Original list: apple banana apple orange banana grape apple
Unique list: apple banana orange grape
(empty)Was this helpful?
Related Questions
- How to use File::Slurp to read entire file into variable in Perl?
- How to get file modification time with Path::Tiny in Perl?
- How to use Path::Tiny touch to create empty file in Perl?
- How to use List::Util reduce for custom aggregation in Perl?
- How to compare two DateTime objects in Perl?
- How to use DateTime::Duration for time periods in Perl?