perldoc > dup(2)

```html

📘 NAME

perlfaq4 – Data Manipulation (removing duplicates) & perlfaq5 – Filehandles (dup()ing filehandles)

🚀 Quick Reference

Use Case Command Description
🔄 Remove duplicates (order not important) my %hash = map { $_, 1 } @array; my @unique = keys %hash; Use a hash to extract unique keys
🔄 Remove duplicates (preserve order) use List::MoreUtils qw(uniq); my @unique = uniq(@array); Use uniq from List::MoreUtils
🔄 Remove duplicates (manual loop) my %seen; my @unique = grep { !$seen{$_}++ } @array; Use a grep with a hash to track seen elements
📂 Dup a filehandle (open) open my $log, '>>', '/foo/logfile'; open STDERR, '>&', $log; Redirect STDERR to an already open log filehandle
📂 Dup a filehandle (numeric fd) my $fd = $ENV{MHCONTEXTFD}; open $mhcontext, "<&=$fd"; Open a filehandle from a numeric file descriptor

🔄 How can I remove duplicate elements from a list or array?

🤝 (contributed by brian d foy)

Use a hash. When you think the words "unique" or "duplicated", think "hash keys".

If you don't care about the order of the elements, you could just create the hash then extract the keys. It's not important how you create that hash: just that you use "keys" to get the unique elements.

my %hash   = map { $_, 1 } @array;
# or a hash slice: @hash{ @array } = ();
# or a foreach: $hash{$_} = 1 foreach ( @array );

my @unique = keys %hash;

If you want to use a module, try the uniq function from List::MoreUtils. In list context it returns the unique elements, preserving their order in the list. In scalar context, it returns the number of unique elements.

use List::MoreUtils qw(uniq);

my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7
my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7

You can also go through each element and skip the ones you've seen before. Use a hash to keep track. The first time the loop sees an element, that element has no key in %Seen. The next statement creates the key and immediately uses its value, which is undef, so the loop continues to the push and increments the value for that key. The next time the loop sees that same element, its key exists in the hash and the value for that key is true (since it's not 0 or undef), so the next skips that iteration and the loop goes to the next element.

my @unique = ();
my %seen   = ();

foreach my $elem ( @array ) {
    next if $seen{ $elem }++;
    push @unique, $elem;
}

You can write this more briefly using a grep, which does the same thing.

my %seen = ();
my @unique = grep { ! $seen{ $_ }++ } @array;

📂 How do I dup() a filehandle in Perl?

If you check "open" in perlfunc, you'll see that several of the ways to call open() should do the trick. For example:

open my $log, '>>', '/foo/logfile';
open STDERR, '>&', $log;

Or even with a literal numeric descriptor:

my $fd = $ENV{MHCONTEXTFD};
open $mhcontext, "<&=$fd";  # like fdopen(3S)

Note that <&STDIN makes a copy, but <&=STDIN makes an alias. That means if you close an aliased handle, all aliases become inaccessible. This is not true with a copied one.

Error checking, as always, has been left as an exercise for the reader.

```
dup(2)
📘 NAME 🚀 Quick Reference 🔄 How can I remove duplicate elements from a list or array? 📂 How do I dup() a filehandle in Perl?

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-16 11:15 @216.73.216.48
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!