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??


You don't need a hash but you do need some way to signal a new group.

my $prev = '';
while ( <FILE> ) {
    my ( $group ) = split;
    next if $group eq $prev;
    # do something with the first
    # line of the new group
    $prev = $group;
    }



John
-- 
use Perl;
program
fulfillment

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

Reply via email to