Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Dealing with nextline of a file

by fishy (Friar)
on Jul 31, 2021 at 20:36 UTC ( [id://11135546]=note: print w/replies, xml ) Need Help??


in reply to Dealing with nextline of a file

Hi jnarayan81,

you can adapt the following code, seen on the web as "Rolling buffer" (by Toby Inkster or Ingy döt Net, can't remember):
For $n=2, on each iteration, $cache[0] and $cache[1] will contain a pair of adjacent lines.
#!/usr/bin/perl ## Go through a stream and prints all but the last n lines where n is +a command line argument: my @cache; my $n = shift @ARGV; while(<>) { push @cache, $_; print shift @cache if @cache > $n; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found