Abhisek has asked for the wisdom of the Perl Monks concerning the following question:

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: Locate Unique values
by GrandFather (Saint) on Feb 12, 2008 at 10:38 UTC

    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
      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
Re: Locate Unique values
by citromatik (Curate) on Feb 12, 2008 at 10:36 UTC

    A very easy way of doing this involve the use of the core modules Tie::File and List::Util:

    use strict; use warnings; use Tie::File; use List::Util qw/first/; tie my @lines, 'Tie::File', shift @ARGV or die $!; my $line15chr = first {/^.{15}$/} @lines; print $line15chr,"\n" if defined $line15chr;

    Hope this helps

    citromatik

Re: Locate Unique values
by hipowls (Curate) on Feb 12, 2008 at 10:44 UTC

    1. The code doesn't compile
    2. You use variables that aren't declared or initialised
    3. Have you used Super Search? This question has been answered many times before

    Your loop would be more idiomatic written as

    foreach my $line ( @lines ) { if ( my ( $pp, $pp1 ) = $line =~ /(.*?)\s+(\S+)/ ) { ... } }

    And yes, you are on the right track