Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
It works as is, but modified to work in HTML I run into problems. I'm using a value based on a radio button to set the number of the record to be deleted but i cant get it to work. If i use a value I type in a text box, it works, but not with the radio button. Any help?Here's a way to do it if your flatfile consists of one record per line +, with whitespace before the "record number" at the end: #!/usr/bin/perl -- -*-fundamental-*- use strict; use Fcntl qw(:flock); ## Set these as needed... my $flatfile = "flat.txt"; my $num2delete = "12"; my $result = &DeleteRecord($flatfile, $num2delete); if ($result) { print "No records were deleted.\n"; } else { print "Deleted record number $num2delete.\n"; } exit; sub DeleteRecord($$) { my $flatfile = shift; my $num2delete = shift; ## Open the flatfile in read/write mode open(FLATFILE, "+< $flatfile") or die "Could not open $flatfile: $!\ +n"; ## Lock it exclusively flock(FLATFILE, LOCK_EX) or die "Could not lock $flatfile: $!\n"; ## Try and find a match my $found=0; while(<FLATFILE>) { last if /\s+$num2delete$/ and ++$found; } ## If no match, we clean up and return a 1 unless ($found) { close(FLATFILE); ## Also unlocks for you return 1; ## Have the caller handle this case; } ## Mark the spot where the record to be deleted starts my $here = tell(FLATFILE) - length $_; ## Read in the rest of the file: my @slurp = <FLATFILE>; ## Rewind to the spot we marked earlier seek(FLATFILE,$here,0); ## Stick everything else back in, overwriting the file as we go: print FLATFILE @slurp; ## If we don't truncate, the last record will appear twice (think ab +out it) truncate(FLATFILE, tell); ## Close and unlock, and return success to the caller close(FLATFILE); return 0; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: modifying records in a delimited text file
by Anonymous Monk on Oct 18, 2000 at 22:47 UTC | |
by Fastolfe (Vicar) on Oct 19, 2000 at 00:20 UTC | |
by Fastolfe (Vicar) on Oct 19, 2000 at 01:01 UTC | |
|
Re: modifying records in a delimited text file
by Fastolfe (Vicar) on Oct 18, 2000 at 22:03 UTC | |
|
Re: modifying records in a delimited text file
by Anonymous Monk on Oct 18, 2000 at 22:25 UTC | |
by Fastolfe (Vicar) on Oct 18, 2000 at 22:32 UTC | |
|
Re: modifying records in a delimited text file
by AgentM (Curate) on Oct 19, 2000 at 01:52 UTC |