Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Putting files in an Array

by jethro (Monsignor)
on Jul 11, 2012 at 15:26 UTC ( #981161=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Putting files in an Array
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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-09-21 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls