Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: foreach question

by OM_Zen (Scribe)
on Feb 25, 2003 at 03:24 UTC ( #238343=note: print w/ replies, xml ) Need Help??


in reply to foreach question

Hi ,

The script with a while to read is :

#!perl use strict; my $ln = 9; my $contents; open (Fnm , "a.dat"); while ($contents = <Fnm>){ chomp; if ($ln == 9){ close Fnm; $ln = 0; } print "[ $contents ]\n"; } __END__ [ one ]


The line that shows is just the first one after closing the contents at the start of the loop

The script with the foreach to read is :
#!perl use strict; my $ln = 9; my $contents; foreach $contents(<Fnm>){ chomp; if ($ln == 9){ close Fnm; $ln = 0; } print "[ $contents ]\n"; } __END__ [ one ] [ next ] [ next ] [ next ] [ next ]


The line that shows is all in the data , even if one closes the file at the start of the foreach , cause the contents are first stored in an anonymous array and then read from that array and hence the foreach is not that one uses to read large chunk of data


Comment on Re: foreach question
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-29 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls