Paul And Joann Van Dalen wrote:

> Hi all,
> 
> Given an input file that has the following records:
> 
> 123    ABC    XX112    YYYY    Zzzzzzzzzz
> 123    DEF    XX113    WWWW    Zzzzzzzzz
> 123    EEF    XX112    YYYY     Zzzzzzzzzz
> 444    ccc    vvbfd    QQQQ    ccccccccc
> 444    CCd    vvbfd    QQQQ    ccccccccc
> 444    ddd    ssddd    QQQQ    xxxxxxxx
> 
> I need to focus on the first column (the input file is already sorted on
> that field) and, grouped by the first column, pull out
> the first record of that group.
> e.g., I would need to have the following from the above as output:
> 
> 123    ABC    XX112    YYYY    Zzzzzzzzzz
> 444    ccc    vvbfd    QQQQ    ccccccccc
> 
> I believe I'd need something like a hash, where for every record within
> a group defined by the common value of the first column, I take the
> numerically first occurance of that group, but I don't know how to do
> that in Perl.  Would it take a loop for each group within the loop for
> the entire file??
> 
> Thanks very much,
> Paul

with a hash, try something like the following:

#!/usr/bin/perl -w
use strict;

my %hash;

open(DATA,'data.txt') || die $!;
while(<DATA>){
        my ($number) = /^(\d+)/;
        next if(exists $hash{$number});
        $hash{$number} = $_;
}
close(DATA);

foreach my $key (sort {$a <=> $b} keys %hash){
        print $hash{$key};
}

__END__

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to