I need to generate statistics from a very large text file (1GB+) and need to process the file line by line. I originally had something like this:
open(FILE, "<$ARGV[0]") || die "$!"; count_bases(20); count_bases(30); ... sub count_bases { seek FILE, 0, SEEK_SET; #reset file pointer my $min_quality = shift; foreach $line (<FILE>) #main loop { my @tmp_ar = split("\t", $line); ... #make comparisons and put #s in hash }

but this ended up being too slow. I run this on a remote server (which I do not have admin) and it always gets killed after locking up the server for a couple of minutes. I looked over a similar script (that I did not write) and noticed that they used while(<>) to read things in. This script was not killed by the server when processing the same large file. So I make the following changes

count_bases(20, $ARGV[1]); count_bases(30, $ARGV[1]); ... sub count_bases { my $min_quality = shift; while(<>) { my @tmp_ar = split(); ... #make comparisons and put #s in hash }

This however does not work for the 2nd count_bases call, it just freezes after the 1st. I am guessing that the while(<>) takes from @ARGV and not the values that I pass into the function so after it uses the 1st time it never starts the end.

my questions: why is the implicit open so much faster? is there a way to get implicit open to work twice? is there a way to make the 1st way fast without implicit open?

thanks


In reply to Efficiently processing a file by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.