Finding last record without slurping.

2002-05-02 Thread Frank Newland

Ladies and Gentlemen:

I'm using a while loop to search for specific record. 
If I find the record, then I need to report that record and the trailer
record.
I'm not slurping the file into memory so I can't use $openfile[$#openfile]
approach. 

Here's what I have:

while () {
if (/searchpattern/) {
print $_ ;
## Now here is where I also need to print the last record.
##  I also need to search the whole file for multiple occurrences of the
pattern.
## I don't have the File::Readbackwards module.
} 

For what its worth, I know that my trailer record has identifying marks in
the first 3 bytes.
Any suggestions?

Thanks,

Frank



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




Re: Finding last record without slurping.

2002-05-02 Thread Michael Lamertz

On Wed, May 01, 2002 at 01:52:35PM -0500, Frank Newland wrote:
> Ladies and Gentlemen:
> 
> I'm using a while loop to search for specific record. 

A record is a single line in your file?

> If I find the record, then I need to report that record and the trailer
> record.

Sorry, insufficient english on my side.  Is the 'trailer' record the one
you read before the current record?

> I'm not slurping the file into memory so I can't use $openfile[$#openfile]
> approach. 

Not necessary.

Use a 2 element circlular list fifo to store the elements while reading.

-- untested --
my $current = 0;
my @stash = (undef, undef);
while () {
chomp;  # not necessary...
$stash[$current] = $_;  # store for later use
if (/YOUR PATTERN/) {
print "Match is '$_'\n";
print "Prev  is '" . $stash[1 - $current] . "'\n";
}
$current = 1 - $current;# toggle for next run
}
-- untested --

The '$current = 1 - $current' is commonly used in games for selecting
sprite coordinates on double-buffered screens :-)

-- 
   If we fail, we will lose the war.

Michael Lamertz|  +49 221 445420 / +49 171 6900 310
Nordstr. 49|   [EMAIL PROTECTED]
50733 Cologne  | http://www.lamertz.net
Germany|   http://www.perl-ronin.de 

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