catfood has asked for the wisdom of the Perl Monks concerning the following question:
my $file="dogy"; open(FH, "+<$file") flock(FH, 2) or die "can't lock $file $!";I then read the file into an array, filter the entries and use seek FH, 0, 0; then I write it back out on the same FH, the problem is that as I print less lines out then existed in the original file, I have the old lines I do not want, I basically want to throw in an EOF after the last entry I write. Here is an example program showing the problem.
#!/bin/perl -w
use strict;
my $file="dogy";
open(FH, "+<$file");
flock(FH, 2);
my @newEntries;
while(my $line = <FH>) {
chomp($line);
if ($line%2==1) { next; }
push(@newEntries,$line);
}
seek FH,0,0;
foreach my $ent (@newEntries) {
print FH $ent . "\n";
}
close(FH)
##############
dogy has 1 2 3 4 5 6 7 8 9 10============ and after running the script I get
2 4 6 8 10 7 8 9 10I want want the 7 - 10 lines to not exist.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: seek in a text file
by ikegami (Patriarch) on Oct 06, 2008 at 00:29 UTC | |
by jwkrahn (Abbot) on Oct 06, 2008 at 04:45 UTC | |
by catfood (Novice) on Oct 06, 2008 at 01:18 UTC | |
|
Re: seek in a text file
by apl (Monsignor) on Oct 06, 2008 at 11:27 UTC | |
|
Re: seek in a text file
by lostjimmy (Chaplain) on Oct 06, 2008 at 01:41 UTC | |
by ikegami (Patriarch) on Oct 06, 2008 at 01:44 UTC | |
|
Re: seek in a text file
by talexb (Chancellor) on Oct 06, 2008 at 17:55 UTC |