| |||||||||||
| |||||||||||
updating a file Posted by baby_perl (baby_perl), 5 March 2005 hello,![]() secondly i have this code below open(INFILE, "topic2.txt") || die "cant find it"; open(OUTFILE, ">new2.txt") || die "cant find it"; while ($line = <INFILE>) { chomp $line; $line =~ s/ {1,}/\n/gi; print OUTFILE "$line\n"; } close(INFILE) || die "cant close it"; close(OUTFILE) || die "cant close it"; the output is the quick brown fox jumped over the lazy dog pls how can i squeeze the space baby perl Posted by admin (Graham Ellis), 6 March 2005 on 03/05/05 at 09:14:33, baby_perl wrote:
If you open a file using "<+ at the start of the file name, you'll open it for read but also with write capability. Simarly you can use >+ and >>+ to open initially in write and append modes. But beware - you probably don't want to do any of these 1. If you write to a file opened in one of these modes, you'll overwrite existing content byte by byte. That's OK for fixed length record data, but such data is rare these days. Replace a line with a shorter line, and you'll leave "droppings". Replace a line with a longer one, and you'll overwrite the start of the next line. 2. If you open with >+, any existing content is delete by the open. 3. You need to use the seek function to move around in the file and you MUST seek when you switch between read and write operations to flush buffers. Better alternative scheme - write a NEW output file, then use Perl's rename function to move the old file to a .bak copy, and the new file to replace the old one. Once you're 100% happy that you application is working, you could replace the first of those two renames with an unlink to get rid os the old file at that stage. Quote:
Add something like if ($line =~ /\S/) onto the end of your print line (before the semicolon). It will then read print OUTFILE "$line\n" if ($line =~ /\S/); That means "print out the line but only if it contains visible (not white space) characters" This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |