On Wed, 2005-10-05 at 10:43, Jeff 'japhy' Pinyan wrote:
> On Oct 5, Charles Farinella said:
> 
> > I have a list of 15,000+ items in a tab separated list.  I am putting
> > all the values into an array, and I need to look through all $array[0]
> > for duplicates and am not sure how to proceed.  Help?
> 
> Whenever you think of frequency, or duplicates, or unique values, think of 
> using a hash.
> 
> The basic idea is:
> 
>    for (LIST) {
>      $frequency{$_}++;
>    }

Thank you for your help, with some help from someone in our office, here
is our solution, perhaps it will be helpful to someone else.

#!/usr/bin/perl -w

use strict;

my %hash;
my $sku;
my $verbose = 0;
my $numDuplicates = 0;

open( INFILE, 'stock.tab' );

foreach( <INFILE> ) {
        my @array = split( "\t", $_ );
            $sku = $array[0];
            $hash{$sku}++;
            # test printout
            print "$sku [$hash{$sku}]\n" if $verbose;
       }

foreach $sku (keys %hash) {
    if( $hash{$sku} > 1) {
        print "$sku: $hash{$sku}\n";
        $numDuplicates++;
    }
}
close INFILE;

print "$numDuplicates duplicates found.\n";
exit $numDuplicates;

> and now your %frequency hash tells you how many times a particular element 
> in the LIST was seen.
> 
>    perldoc -q duplicate
> 
> Once you've looked that over and come up with some code, show us and 
> we'll guide you from there.
> 
> -- 
> Jeff "japhy" Pinyan        %  How can we ever be the sold short or
> RPI Acacia Brother #734    %  the cheated, we who for every service
> http://www.perlmonks.org/  %  have long ago been overpaid?
> http://princeton.pm.org/   %    -- Meister Eckhart
-- 
Charles Farinella 
Appropriate Solutions, Inc. (www.AppropriateSolutions.com)
[EMAIL PROTECTED]
603.924.6079


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to