John, Lighten up matey...
The examples I've given are just that examples... Error checking functionality is up the end programmer... (And you are quite right, you should check the status of operations.) This is a beginers list, let's keep it friendly, eh? ... and FWIW ... for my $line (<INFILE>) { # stuff } is equivalent to: while (my $line = <INFILE>) { # stuff } If you are going to correct me, at least get it right and do it in a friendly manner! ;) :P :) Regards, Michael S. E. Kraus Software Developer Wild Technology Pty Ltd _______________________________ ABN 98 091 470 692 Level 4 Tiara, 306/9 Crystal Street, Waterloo NSW 2017, Australia Telephone 1300-13-9453 | Facsimile 1300-88-9453 http://www.wildtechnology.net The information contained in this email message and any attachments may be confidential information and may also be the subject of client legal - legal professional privilege. If you are not the intended recipient, any use, interference with, disclosure or copying of this material is unauthorised and prohibited. This email and any attachments are also subject to copyright. No part of them may be reproduced, adapted or transmitted without the written permission of the copyright owner. If you have received this email in error, please immediately advise the sender by return email and delete the message from your system. > -----Original Message----- > From: John W. Krahn [mailto:[EMAIL PROTECTED] > Sent: Thursday, 25 November 2004 11:17 AM > To: Perl Beginners > Subject: Spam:Re: Using regular expressions > > Michael S. E. Kraus wrote: > > G'day... > > Hello, > > > On Wed, 2004-11-24 at 23:10, FlashMX wrote: > > > >>To be able to do a grep on a file via a perl script do you have to > >>read the whole file in before performing the search and replace? > >>I've been hearing that reading the whole file in takes up > memory and > >>if multiple users are running the script then you better > have alot of > >>swap and memory. > >> > >>Is this correct? > > > > Yes and No... You can read a whole file in and perform > changes, and > > then write out the differences to the same filename... > > > > This may or may not take up much memory... a 23K file is only 23K > > after all... it all depends on your averages... > > > >>If this is the case how else could I do a grep to do a search and > >>replace without using all the resources? > > > > You can also read a file in a line at a time, perform the search and > ^^^^^^^^^^^^^^^^ > > replace on each line, and write the output to a new file... then at > > the end replace the old file with the new one... > > > > E.g. > > > > open (INFILE, "< $filename"); > > open (OUTFILE, "> $filename.$$"); > > You should *always* verify that the files were opened correctly. > > open INFILE, '<', $filename or die "Cannot open $filename: $!"; > open OUTFILE, '>', "$filename.$$" or die "Cannot open > $filename.$$: $!"; > > > > for my $line (<INFILE>) { > > You imply above that you will read the file "a line at a > time" however using a for loop will read the whole file into > a list in memory. You need to use a while loop to read a > line at a time. > > while ( my $line = <INFILE> ) { > > > > $line =~ s/old/new/gi; > > print OUTFILE $line; > > } > > > > close(OUTFILE); > > close(INFILE); > > > > rename("$filename.$$","$filename"); > > You should verify that rename worked correctly > > rename "$filename.$$", $filename or die "Cannot rename > $filename.$$: $!"; > > > > John > -- > use Perl; > program > fulfillment > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] For > additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>