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

Re: how to populate array with last 20 lines of file

by dasgar (Priest)
on Aug 08, 2017 at 18:56 UTC ( #1197028=note: print w/replies, xml ) Need Help??


in reply to how to populate array with last 20 lines of file

I don't know if this is the "best" way, but one route would be to use Tie::File. It basically "represents a regular text file as a Perl array". And since it does not store the entire file into memory, you can use this for large files without major concern about memory usage - although the 8KB size you mentioned in another post isn't too big to store in memory.

If you use Tie::File, grabbing the last 20 lines is simply grabbing the last 20 elements of the array.

  • Comment on Re: how to populate array with last 20 lines of file

Replies are listed 'Best First'.
Re^2: how to populate array with last 20 lines of file
by ytjPerl (Scribe) on Aug 08, 2017 at 19:51 UTC
    Hi, Thanks for your posting. The major concern of not using the whole file is not because of memory issue. I need to find a string in that file to determine whether application is running or not. If I restart the application multiple times, there will be multiple 'startup' string in this log, so that I only need last lines of the log when its size is not increasing anymore.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-09-16 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?