Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Going through a big file

by LanX (Canon)
on Jan 17, 2013 at 12:37 UTC ( #1013769=note: print w/ replies, xml ) Need Help??


in reply to Going through a big file [solved]

> First I tried the obvious

open in, shift @ARGV; for(<in>){

Obvious?

You will hardly find any example in the perldocs ever trying this.

Rather

open in, shift @ARGV; while (<in>){

or better

open my $in, '<', shift @ARGV; while (my $line = <$in>){

Explanation

your code is semantically equivalent to

my @temp_list=<in>; for(@temp_list){
slurping the whole file as a first step.

for expects a list and evaluates in list context╣, so it's greedily swallowing all at once.

But while iterates in scalar context, that is line by line (as defined by '$/')

Cheers Rolf

╣) with a little magical exception in recent Perl versions (>= 5.8 ?) regarding ranges, which isn't relevant here


Comment on Re: Going through a big file
Select or Download Code
Re^2: Going through a big file
by Chuma (Acolyte) on Jan 17, 2013 at 14:05 UTC
    Oh! Well, that explains it. Thanks very much!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2015-07-06 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (80 votes), past polls