Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Performing a tail(1) in Perl (reading the last N lines of a file)

by broquaint (Abbot)
on Apr 25, 2002 at 17:18 UTC ( [id://162046]=note: print w/replies, xml ) Need Help??


in reply to Performing a tail(1) in Perl (reading the last N lines of a file)

I'm not sure how this benchmarks, but it is a little more succinct ;-)
use Tie::File; my $length = shift @ARGV; my $file = shift @ARGV; tie(my @file, "Tie::File", $file, autochomp => 0) or die("ack - $!"); print @file[$#file - $length + 1 .. $#file];
_________
broquaint

Update: fixed slice code as it was off by one (as noted by grinder)

  • Comment on Re: Performing a tail(1) in Perl (reading the last N lines of a file)
  • Download Code

Replies are listed 'Best First'.
Re: Performing a tail(1) in Perl (reading the last N lines of a file)
by Dominus (Parson) on Apr 26, 2002 at 03:37 UTC
    Says broquaint:
    I'm not sure how this benchmarks, but it is a little more succinct ;-)
    use Tie::File; ... print @file[$#file - $length + 1 .. $#file];
    It should benchmark OK, although not as well as a special-purpose backward-file-reader. But I agree with you about the succinctness. I mostly followed up to point out that you can do even better:
    print @file[-$length .. -1];
    Hope this helps.

    --
    Mark Dominus
    Perl Paraphernalia

Log In?
Username:
Password:

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

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

    No recent polls found