On Thu, Aug 09, 2001 at 11:35:13AM -0500, Frank Newland wrote:
> Readers:
> awk has a function  called 'getline' which reads the next line of input
> without changing control of the script.

Perl is unlike sed and awk in this respect; program control is not 
determined by reading sequential lines of input.  Program control
in Perl is determined by use of control structures (if, while, foreach, etc.).

> My input file contains pairs of records. When I find a record that matches
> my search pattern, I want that record and the next record. 
> In awk, I used the 'getline' which did just that.
> 
> How can I do this in perl?
> Does using the $* variable get me there?
> --------------------------------------------------
> #!/usr/bin/perl -w
> use strict ;
> ## If the record ends in 03 then capture this record and the next one.
> while (<>) {
> chomp ;
> 
> if ( /03$/)  {
> print ; #print this record, now how do I capture just the next record?
> }
> ------------------------------------------------------------

It's difficult to tell what needs to be done from your description
without seeing the sample input and output.  It sounds like you
want to print odd rows when they end in '03', followed by the even
row immediately following.  When odd rows do not contain '03', you
want to skip both.  This would do that:

while(<>) {
        my $next_record = <>;  ## Read in the next line

        next unless /03$/;     ## do nothing unless we want to print the pair

        print;                 ## print the odd row (ending in 03)
        print $next_record;    ## print the even row (immediately following 03)
}

HTH,

Z.


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

Reply via email to