in reply to Locate Unique column from a given set of column

Who sold you this junk? The wheels are square and they don't even turn!

Ummm, I mean, you use strict; which is excellent, but there are eight variables used that are not declared. Even if the obvious declarations are made there are three variables that are used without being initialized!

It may be that the following code (completely unrelated to your sample) may illustrate the technique you are asking about:

#!/usr/bin/perl -w use strict; my @lines = split "\n", <<"LINES"; 12345678901234 123456789012345 1234567890123456 123456789012xxx LINES chomp @lines; my @lines15 = grep {15 == length} @lines; print join "\n", @lines15;

Prints:

123456789012345 123456789012xxx

Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: Locate Unique values
by Abhisek (Novice) on Feb 13, 2008 at 05:48 UTC
    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); } }

      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