
inlimbo
User
Dec 3, 2004, 10:48 PM
Post #1 of 8
(2189 views)
|
|
Easy flatfile DB Question2: overwrite with conditions
|
Can't Post
|
|
A follow up question to my last post... Ok I have a simple flatfile database deliminated by a pipe (|). It has 3 columns. I pass in _three_ variables using CGI.pm, lets call em $value1, $value2 and $value3. If there is a _line_ in the database where the first column = $value1 _AND_ (!) the second column = $value2 then I want to _REPLACE_ the value in the _THIRD_ column with $value3. At the moment it appends the file, rather than overwriting the line. My code below, is wrong and im sure their is a more efficient way of doing it (using fcntl ':flock' ?) Thanx in advance [again:)] inlimbo....
#!/perl/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Fcntl ':flock'; print header(); print start_html("Overwrite Line of DB"); print "hello\n"; my $value1 = param('value1'); my $value2 = param('value2'); my $value3 = param('value3'); open(INF,"test.txt") or &dienice("Can't open test.txt: $!"); my @db = <INF>; close(INF); foreach my $i (@db) { chomp($i); ($column1,$column2,$column3) = split(/\|/,$i); if ($column1 eq $value1 and $column2 eq $value2) { $column3 = $value3; open(OUTF,">>test.txt") or dienice("Can't open test.txt for writing: $i"); flock(OUTF,2); seek(OUTF,0,2); print OUTF "$column1|$column2|$column3\n"; close(OUTF); } } sub dienice { my($errmsg) = @_; print "<h2>Error</h2>\n"; print "<p>$errmsg</p>\n"; print end_html; exit; }
|