in reply to finding the distinct elements in an array

While it's a fairly common idiom to produce, and the usual manner is to use a hash to filter uniques, it's less error-prone to simply use the common module, List::MoreUtils, with its uniq() method.

use strict; use warnings; use List::MoreUtils qw( uniq ); my @array = (1, 1, 3, 2, 8, 9, 4, 2, 3); print uniq( @array ), "\n";

Dave

Replies are listed 'Best First'.
Re^2: finding the distinct elements in an array
by jdtoronto (Prior) on May 29, 2006 at 16:21 UTC
    Which.. guess what..
    sub uniq (@) { my %h; map { $h{$_}++ == 0 ? $_ : () } @_; }
    uses a hash!

    Whilst in principal using a module is often a good thing, maybe it would be better if the OP understood what was going on under the hood first?

    Just my not so humble opinion.

    jdtoronto