The problem is with the variable $txt! You are loading all the file inside the $txt and only after this you are counthing the "words".

Other tinhg, your regexp (RE) are wrong! [] is to make a group of characters! For example, \w is the same of [a-zA-Z0-9], and you don't need to put the "\" before "," and ":" too. I thing that the right RE inside split is: / +\n+,:/

Since you are using a big file (150M) win speed is good too! Don't read the file using <TEXT>, use read() or sysread(), because <> will scan the data and look for \n, and only after this return the data!

You dont need to use this: my @array = (); to clean the array! Because when you make: my @array ; you already have cleanded the array!

Try this code for your script:

$fname = "$0"; open(TEXT, "<$fname")|| die "could not open file: $fname\n"; ## Let's read 100KB per time. my $buffer_size = 1024*100 ; my ($buffer,$words) ; while (sysread TEXT, $buffer, $buffer_size) { ## This will count fast for you: my @c = ($buffer =~ /( +\n+,:)/gs) ; $words += @c ; } close TEXT; print "Length: $words\n" ; exit;
"The creativity is the expression of the liberty".

In reply to Re: Out of memory by gmpassos
in thread Out of memory 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.