even better... __________________
On Thursday, April 11, 2002, at 01:17 PM, Timothy Johnson wrote: > > Just for those who haven't figured out why the first example is so useful, > consider what perl is doing in the following two snippets: > > 1 > ====== > open(INFILE,"myfile.txt"); > @file = <INFILE>; > foreach(@file){ > do something to $_... > } > > 2 > ====== > open(INFILE,"myfile.txt"); > while(<STDIN>){ > do something to $_... > } > assume you meant: while(<INFILE>) > In the first example, Perl will load the entire contents of the file into > memory, then perform any operations on the contents. This can really bog > down your system, especially if you have, say, a 500MB file to parse. The > second example loads the file one line at a time. I learned this lesson > the > hard way. > > of course, you could do: open(INFILE,"myfile.txt"); for (<INFILE>) { # do something with $_ } but, that isn't going to help understanding :) > -----Original Message----- > From: drieux [mailto:[EMAIL PROTECTED]] > Sent: Thursday, April 11, 2002 12:25 PM > To: [EMAIL PROTECTED] > Subject: Re: while and do > > > > On Thursday, April 11, 2002, at 12:02 , Teresa Raymond wrote: > >> Could someone explain the while and do w/ couple of examples? I have yet >> to use them, only using the foreach loop and if else stmts. > > the obvious ones are > > while(<STDIN>) { > > # bunch of stuff one wants to do with the STDIN stuff > } > > or my all time basic > > while ( my ( $key, $val ) = each %someHash ) { > > # based upon key or val do the stuff for all elements of the > hash > } > > in the main I do not do 'do' things... > > ciao > drieux > > --- > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > -------------------------------------------------------------------------------- > This email may contain confidential and privileged > material for the sole use of the intended recipient. > If you are not the intended recipient, please contact > the sender and delete all copies. > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]