http://www.perlmonks.org?node_id=981161


in reply to Putting files in an Array

ARGV in the while condition needs a '@'. Did you ever try to execute the script? UPDATE: Please ignore this line, I must have been in auto-destruct mode ;-). Thanks monsoon for the speedy correction

Naturally you could use a hash as well. Whether it works better with a hash or an array depends on what you need to do with the data. It also depends on the data. Since you only put lines into @results if they have "head" somewhere in the line and your example text file has not one line with "head" in it, an empty hash is as good as an empty array ;-)

PS: Your script seems to be a copy of a script suggested to you in this thread Out of Memory in Perl. I hope Zentara forgives me for saying this, but his advice to use @ARGVs magic just to read a file line by line is misleading. First of all it won't work to save memory by reading a file line by line if you then store it into an array anyway. And secondly you can read a file line by line without using @ARGV magic

Your original script (with the bug corrected) would have done the job as well.

Replies are listed 'Best First'.
Re^2: Putting files in an Array
by monsoon (Pilgrim) on Jul 11, 2012 at 15:31 UTC
    <@ARGV> won't work. ARGV is a special filehandle. See here
Re^2: Putting files in an Array
by zentara (Archbishop) on Jul 11, 2012 at 17:57 UTC
    First of all it won't work to save memory by reading a file line by line if you then store it into an array anyway.

    Sorry if I was misleading. In the original post, IIRC, he complained about filtering 2 gig files. The ARGV trick pushed matches into a @results array, and unless he had 2 gig's worth of matches, he should not run out of memory. In this node, he seems to want to slurp all lines into the array. I think that caused confusion, because in this node he wanted to use the ARGV trick on small files.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh