in reply to Re: Locate Unique values
in thread Locate Unique column from a given set of column

After locating the 15 character long values .There are when i have the same 15 character long value reapeated number of times !!!! Now i wish to find only the unique ones occurence of it. I found this code on the web. What do you suggest me here.
%seen = (); @uniq = (); foreach $item (@proc_name1) { unless ($seen{$item}) { $seen{$item} = 1; push (@uniq, $item); } }

Replies are listed 'Best First'.
Re^3: Locate Unique values
by GrandFather (Saint) on Feb 13, 2008 at 07:48 UTC

    First off: always use strictures (use strict; use warnings;).

    Second: you don't need to initialize arrays and hashes. Your declarations should look like:

    my %seen; my @unique;

    Avoid extra nesting. Refactor the loop as:

    foreach my $item (@proc_name1) { next if $seen{$item}; $seen{$item} = 1; push (@uniq, $item); }

    However, if you don't need to know the order that the items were seen then you don't need the array and can just:

    my %seen; $seen{$_}++ for @proc_name1;

    or using a hash slice you could:

    my %seen; @seen{@proc_name1} = ();

    In either case keys %seen gives the list of unique proc names.


    Perl is environmentally friendly - it saves trees