Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Loading a part of the file to array using Tie::File

by roboticus (Chancellor)
on Nov 21, 2017 at 15:15 UTC ( [id://1203893]=note: print w/replies, xml ) Need Help??


in reply to Loading a part of the file to array using Tie::File

ansh007:

The problem with memory is that you're reading all the lines of the file at once. Rather than doing that, read the file line by line and do your processing. That way you can handle a file of any size. You can do so like this:

# The three-argument version of open, and using lexical variables is p +referred open my $LOG_READ, '<', $InLogFilePath or die "Can't open file $InLogF +ilePath: $!"; # Read the next line into $current_line while (my $current_line = <$LOG_READ>) { # Ignore the first $InStartLineNumber lines of the file next if $. < $InStartLineNumber; # Process current line . . . } close $LOG_READ;

As you'll notice, I intentionally changed the way you did things:

There's no real need to spawn two additional processes to perform tasks that perl can already do for you. Especially not to:

  • Earn a "useless use of cat" award.
  • Use tail to ignore some of your input file, a task perl is perfectly capable of itself.

The three-argument form of open is safer than the old two-argument open.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-29 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found