Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Split on new line

by McDarren (Abbot)
on Feb 22, 2009 at 08:16 UTC ( #745607=note: print w/ replies, xml ) Need Help??


in reply to Split on new line

You have an answer to your immediate problem, but there is something else that you may wish to consider.

Depending on the size of your log file, and the specs of your machine, reading the entire log file into an array may not be such a good idea. This is because memory must be allocated for the entire file. And if you happen to be dealing with a multi-gigabyte file, this may become a problem.

Generally, a better approach is to read the file line by line, and process each line as you go. For this, a while loop can be used. Example:

#!/usr/bin/perl use strict; use warnings; # 3 argument form of open using a lexical variable is considered bette +r practice # see perldoc -f open open my $file, '<', $ARGV[0] or die "Can't open $ARGV[0] for reading: +$!\n"; while (my $line = <$file>) { print $line; chomp($line); #if you need to # any other processing.... }
Using the above method, only one line of the file is read into memory at a time, which is much more memory-efficient.

Hope this helps,
Darren :)


Comment on Re: Split on new line
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-07-30 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls