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

Re: foreach question

by OM_Zen (Scribe)
on Feb 25, 2003 at 03:24 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found