Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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 taking refuge in the Monastery: (20)
As of 2014-07-22 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (116 votes), past polls