in reply to Unique elements in array

It's better if you can avoid an unnecessary intermediary array, e.g. in a typical case of reading ids from the first column in a tsv file, I'd go for something like:
my %unique; my @ids = map { chop; my @fld = split /\t/; !defined( $unique{ $fld[0] } ) && $unique{ $fld[0] }++ && $fld[0]; } <$fileHandle>;
__________________________________________________________________________________

^M Free your mind!

Replies are listed 'Best First'.
Re^2: Unique elements in array
by chromatic (Archbishop) on May 09, 2007 at 17:28 UTC

    Ouch. chop is almost always wrong (what if your EOL marker is more than one character)--and what if $fld[0] is a valid value that evaluates to false in boolean context?

      The popular misuse of chomp() deserves a new topic. As for $fld[0] - defined( unique{ $fld[0] } ) will be true in the case you cite.

      There might be functional issues or data quality issues in any real example but these are in general OT.

      __________________________________________________________________________________

      ^M Free your mind!