CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Easy flatfile DB Question2: overwrite with conditions

 



inlimbo
User

Dec 3, 2004, 10:48 PM

Post #1 of 8 (2911 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....



Code
#!/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;
}



KevinR
Veteran


Dec 4, 2004, 12:43 AM

Post #2 of 8 (2910 views)
Re: [inlimbo] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post

the reason the file is being appended to is because you have opened the file in append to mode ">>".

You probably don't need to use the fcntl module just to flock a file, a simple flock() function should do. Since you are reading the entire file into an array (@db) first, I would first check the array and see if the conditions are true, and if so, open the file for overwriting. If not, you don't need to reopen the file. Something like this:


Code
#!/perl/bin/perl -wT  
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
#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');
my $flag = 0;

open(INF,"test.txt") or &dienice("Can't open test.txt: $!");
my @db = <INF>;
close(INF);

foreach (@db) {
chomp;
my ($c1,$c2,$c3) = split(/\|/);
($c3,$flag) = ($value3,$flag+=1) if ($c1 eq $value1 and $c2 eq $value2);
}

if ($flag > 0) {
open(OUTF,">test.txt") or &dienice("Can't open test.txt: $!");
flock(OUTF,2);
print OUTF "$_\n" for @db;
close(OUTF);
print "$flag lines were overwritten";
}

else {
print "No lines were overwritten";
}

print end_html;

sub dienice {
my($errmsg) = @_;
print "<h2>Error</h2>\n";
print "<p>$errmsg</p>\n";
print end_html;
exit;
}

-------------------------------------------------


inlimbo
User

Dec 4, 2004, 3:26 AM

Post #3 of 8 (2904 views)
Re: [KevinR] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post

Thanks KevinR, but there is a slight glitch.

It comes up with the correct messages, saying "1 lines were overwritten" when the conditions are true and "No lines were overwritten" when the conditions are false. BUT when the conditions are true it _doesn't_ overwrite the line in the database - the file is exactly the same as it was.

Any ideas?

Cheers
inlimbo...


KevinR
Veteran


Dec 4, 2004, 10:46 AM

Post #4 of 8 (2899 views)
Re: [inlimbo] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post

oops, I think I only changed the split variable and not the line in the array, should be:


Code
foreach (@db) {  
chomp;
my ($c1,$c2,$c3) = split(/\|/);
($_,$flag) = ("$c1\|$c2\|$value3",$flag+=1) if ($c1 eq $value1 and $c2 eq $value2);
}

-------------------------------------------------


(This post was edited by KevinR on Dec 4, 2004, 10:52 AM)


inlimbo
User

Dec 4, 2004, 2:22 PM

Post #5 of 8 (2893 views)
Re: [KevinR] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post

Thanks Kevin!!!


davorg
Thaumaturge / Moderator

Dec 5, 2004, 11:07 AM

Post #6 of 8 (2888 views)
Re: [KevinR] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post


Quote
You probably don't need to use the fcntl module just to flock a file, a simple flock() function should do.


inlimbo is using the Fcntl module to import the :flock constants. It's always a good idea to use symbolic constants instead of "magic numbers" in code as it makes it clearer what is going on.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Dec 5, 2004, 1:54 PM

Post #7 of 8 (2887 views)
Re: [davorg] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post


In Reply To

Quote
You probably don't need to use the fcntl module just to flock a file, a simple flock() function should do.


inlimbo is using the Fcntl module to import the :flock constants. It's always a good idea to use symbolic constants instead of "magic numbers" in code as it makes it clearer what is going on.


Yes, I understand what you're getting at, but they were still using the 'magic numbers' anyway in their code, were they not?
-------------------------------------------------


davorg
Thaumaturge / Moderator

Dec 5, 2004, 1:58 PM

Post #8 of 8 (2886 views)
Re: [KevinR] Easy flatfile DB Question2: overwrite with conditions [In reply to] Can't Post

Ah yeah. You're right, of course :-/

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives