{
    "mode": "perldoc",
    "parameter": "dup",
    "section": "-q",
    "url": "https://www.chedong.com/phpMan.php/perldoc/dup/json",
    "generated": "2026-06-03T03:35:12Z",
    "sections": {
        "Found in /usr/share/perl/5.34/pod/perlfaq4.pod": {
            "content": "How can I remove duplicate elements from a list or array?\n(contributed by brian d foy)\n\nUse a hash. When you think the words \"unique\" or \"duplicated\", think\n\"hash keys\".\n\nIf you don't care about the order of the elements, you could just create\nthe hash then extract the keys. It's not important how you create that\nhash: just that you use \"keys\" to get the unique elements.\n\nmy %hash   = map { $, 1 } @array;\n# or a hash slice: @hash{ @array } = ();\n# or a foreach: $hash{$} = 1 foreach ( @array );\n\nmy @unique = keys %hash;\n\nIf you want to use a module, try the \"uniq\" function from\nList::MoreUtils. In list context it returns the unique elements,\npreserving their order in the list. In scalar context, it returns the\nnumber of unique elements.\n\nuse List::MoreUtils qw(uniq);\n\nmy @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7\nmy $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7\n\nYou can also go through each element and skip the ones you've seen\nbefore. Use a hash to keep track. The first time the loop sees an\nelement, that element has no key in %Seen. The \"next\" statement creates\nthe key and immediately uses its value, which is \"undef\", so the loop\ncontinues to the \"push\" and increments the value for that key. The next\ntime the loop sees that same element, its key exists in the hash *and*\nthe value for that key is true (since it's not 0 or \"undef\"), so the\nnext skips that iteration and the loop goes to the next element.\n\nmy @unique = ();\nmy %seen   = ();\n\nforeach my $elem ( @array ) {\nnext if $seen{ $elem }++;\npush @unique, $elem;\n}\n\nYou can write this more briefly using a grep, which does the same thing.\n\nmy %seen = ();\nmy @unique = grep { ! $seen{ $ }++ } @array;\n",
            "subsections": []
        },
        "Found in /usr/share/perl/5.34/pod/perlfaq5.pod": {
            "content": "How do I dup() a filehandle in Perl?\nIf you check \"open\" in perlfunc, you'll see that several of the ways to\ncall open() should do the trick. For example:\n\nopen my $log, '>>', '/foo/logfile';\nopen STDERR, '>&', $log;\n\nOr even with a literal numeric descriptor:\n\nmy $fd = $ENV{MHCONTEXTFD};\nopen $mhcontext, \"<&=$fd\";  # like fdopen(3S)\n\nNote that \"<&STDIN\" makes a copy, but \"<&=STDIN\" makes an alias. That\nmeans if you close an aliased handle, all aliases become inaccessible.\nThis is not true with a copied one.\n\nError checking, as always, has been left as an exercise for the reader.\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}