Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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 surveying the Monastery: (13)
As of 2014-09-23 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (221 votes), past polls