This is where you need to pad spaces. So, for example: - old: "I am young and fast\n" (20 bytes)
- new: "I am old and slow\n" (18 bytes) The second version is shorter, so it will need two spaces before the newline: - new: "I am old and slow__\n" (20 bytes; I put "_" instead of a space) There is a problem here that I didn't consider. If your line-length INCREASES, then this whole idea is, sadly, rubbish :( -----Original Message----- From: Tom Reed <t...@dkinbox.com> Sent: Wednesday, May 31, 2023 11:31 AM To: Claude Brown <claude.br...@gigacomm.net.au> Cc: beginners@perl.org Subject: RE: storage types for big file Hello I am not sure, <change $line, making sure the length (in bytes) doesn't change> what does this mean? not changing the line length? but in most case I need to change the length of a line. Thanks again. Tom > That sounds positive. You should be able to avoid most of the overhead of > writing the file. The general idea is that you are "updating in place" > rather than writing out the whole file. > > Something like below is what I was thinking, but it isn't tested! Make > sure you have a copy of your input file. Oh, and I haven't thought about > "wide" characters which may influence how some of this works. > > Cheers, > > Claude. > > > open(my $fh, "+<", $theFileName) or die "open: $!"; > > my $startOfLine = tell($fh) or die "tell: $!; > while (defined(my $line = <$fh>)) > { > if ($line has content that needs to change) > { > <change $line, making sure the length (in bytes) > doesn't change> > seek($fh, $startOfLine, 0) or die "seek: $!"; > print $fh $line; > } > > $startOfLine = tell($fh) or die "tell: $!; > } > > close($fh); > > > -----Original Message----- > From: Tom Reed <t...@dkinbox.com<mailto:t...@dkinbox.com>> > Sent: Wednesday, May 31, 2023 9:33 AM > To: Claude Brown > <claude.br...@gigacomm.net.au<mailto:claude.br...@gigacomm.net.au>> > Cc: beginners@perl.org<mailto:beginners@perl.org> > Subject: RE: storage types for big file > > > >> Do you have the option to "seek" to the correct place in the file to >> make >> your changes? For example, perhaps: >> >> - Your changes are few compared to writing out the whole file >> - Your changes do not change the size of the file (or you can pad >> line-end >> with spaces) >> > > 1. each time just changes few lines, not the full file. > 2. it has file size changed, but yes I can pad line-end with space. > > So what's the further? > > Thanks > > > -- > Sent from https://dkinbox.com/ > > > -- > To unsubscribe, e-mail: > beginners-unsubscr...@perl.org<mailto:beginners-unsubscr...@perl.org> > For additional commands, e-mail: > beginners-h...@perl.org<mailto:beginners-h...@perl.org> > http://learn.perl.org/ > > > -- Sent from https://dkinbox.com/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org<mailto:beginners-unsubscr...@perl.org> For additional commands, e-mail: beginners-h...@perl.org<mailto:beginners-h...@perl.org> http://learn.perl.org/