Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Loading a part of the file to array using Tie::File by roboticus
in thread Loading a part of the file to array using Tie::File by ansh007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found