Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > appending to file appends twice!?

Reply
Thread Tools

appending to file appends twice!?

 
 
Guy
Guest
Posts: n/a
 
      12-05-2003
Hi all,

I wrote a Perl script to append Questions and Answers to a text data file.
The file would look like this and would grow when Q&As are added.
1 Q This is a question
1 A This is an answer
2 Q This is another question
2 A This is another answer

All I do is open the file for append, print 2 lines (Q line and A line) and
then close the file. What's happening however is that my data is appended
twice, like this: This is an actual cut & Paste of the entire file.

1 Q Here is my question
1 A Hee is my answer
2 Q Here is my question
2 A Hee is my answer
3 Q here is another question
3 A this is another answer
4 Q here is another question
4 A this is another answer
5 Q qqqqq
5 A zaaaaaa
6 Q qqqqq
6 A zaaaaaa
7 Q sdfkhj asdlfh fih asdfh ;alsdfh
7 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
8 Q sdfkhj asdlfh fih asdfh ;alsdfh
8 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef

What's worst is that you notice on the above file that the count does
increment as if my routine is called twice. So what I've done was added a
few check-point print commands inside the routine and these only print once,
as if my routine is only called once.

Any ideas,
Guy

FYI
They tell me the server is a SUN Sparc V100 with dual 100mb ports.
The Web is provided by an iPlanet webserver.
It is Perl 5.008

Curious
Here is the code for the updating routine...

sub f25
{
my $chk=0;
my $number=0;
my $qaline;
my @qafield;
&prt_head; # Prints HTML code back to browser
$chk=get_qa(); # see below
if($chk==0)
{
foreach $qaline (@qafile)
{
@qafield=();
push(@qafield,split(/\t/,$qaline));
$number=$qafield[0];
}
print"Number = $number <BR> \n"; # checkpoint print command
$number++;
print"Number=$number<BR><BR><BR>\n"; # checkpoint print command
$chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,">>$qafil"))
{
print"Added<BR>\n";
print FILE "$number\tQ\t$fq\t\n$number\tA\t$fa\t\n";
close(FILE);
print"Done<BR>\n";
$chk=0;
}
}
}
if($chk != 0){erreur($chk);}
print"</FORM></BODY></HTML>\n";
}

sub get_qa
{
my $chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,"$qafil"))
{
@qafile=<FILE>;
close(FILE);
$chk=0;
}
}
return $chk;
}


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-06-2003
Guy <(E-Mail Removed)> wrote:

> if($chk==0)



Space characters are not a scarce resource.

Feel free to use as many as you like to make your code
easier to read and understand:

if ( $chk == 0 )


> @qafield=();
> push(@qafield,split(/\t/,$qaline));
> $number=$qafield[0];



You can replace those 3 lines with one line that uses a "list slice":

my $number = (split(/\t/,$qaline))[0]; # untested


> if(-d "$fd")

^ ^
^ ^
A useless use of double quotes:

perldoc -q quot

What's wrong with always quoting "$vars"?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Walt Mankowski
Guest
Posts: n/a
 
      12-06-2003
On 2003-12-05, Guy <(E-Mail Removed)> wrote:
> Hi all,
>
> I wrote a Perl script to append Questions and Answers to a text data file.
> The file would look like this and would grow when Q&As are added.
> 1 Q This is a question
> 1 A This is an answer
> 2 Q This is another question
> 2 A This is another answer
>
> All I do is open the file for append, print 2 lines (Q line and A line) and
> then close the file. What's happening however is that my data is appended
> twice, like this: This is an actual cut & Paste of the entire file.
>
> 1 Q Here is my question
> 1 A Hee is my answer
> 2 Q Here is my question
> 2 A Hee is my answer
> 3 Q here is another question
> 3 A this is another answer
> 4 Q here is another question
> 4 A this is another answer
> 5 Q qqqqq
> 5 A zaaaaaa
> 6 Q qqqqq
> 6 A zaaaaaa
> 7 Q sdfkhj asdlfh fih asdfh ;alsdfh
> 7 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
> 8 Q sdfkhj asdlfh fih asdfh ;alsdfh
> 8 A asdkfj; alsdfh ausf iaufh jkahf kawhef awef
>
> What's worst is that you notice on the above file that the count does
> increment as if my routine is called twice. So what I've done was added a
> few check-point print commands inside the routine and these only print once,
> as if my routine is only called once.
>
> Any ideas,


The code looks OK, so chances are it is being called twice somehow.

This looks like CGI code. Is it possible the script itself is being
called twice? What do your weblogs say?

Walt
 
Reply With Quote
 
Guy
Guest
Posts: n/a
 
      12-07-2003
Well I think I found the cause, but I still don't understand what's wrong...
(BTW, I will read up on perldoc -q quot, thanks for that

I recently added a locking scheme to prevent 2 instances of my Perl script
from updating the text data files at the same time.

Basically it locks a dummy file for exclusive - once the file is locked it
does its stuff - then it closes that dummy file.

In any case, it turns out that if I don't call those locksys and unlocksys
routines of mine, this problem doesn't occur.
Am I doing anything wrong.
The only thing I can see different is that I have 2 files open at the same
time: LCKFILE and FILE
Do I need to so anything before opening a 2nd file or using a 2nd file
handle?

sub locksys
{
my $chk=3840;
if(-d "$fd")
{
$chk=3584;
if(open(LCKFILE,">$lockfil"))
{
$chk=flock(LCKFILE,2);
}
}
return $chk;
}

sub unlocksys
{
close LCKFILE;
}

---------------------
Once that dummy file is locked, I open , read, and close other files using
FILE, for example:
--------------------

sub get_qa
{
my $chk=3840;
if(-d "$fd")
{
$chk=256;
if(open(FILE,"$qafil"))
{
@qafile=<FILE>;
close(FILE);
$chk=0;
print"QAFILE = @qafile<BR><BR>\n";
}
}
return $chk;
}

sub f25
{
my $chk=0;
my $number=0;
my $qaline;
my @qafield;

&prt_head;

$chk=get_qa();
if($chk==0)
{
$chk=256;
if(open(FILE,">$qafil"))
{
$chk=1;
foreach $qaline (@qafile)
{
@qafield=();
push(@qafield,split(/\t/,$qaline));
if($qafield[0] == $fn)
{
$qaline="$fn\t$fq\t$fa\t\n";
$chk=0;
}
print FILE $qaline;
}
if($chk==1)
{
$fn=$qafield[0]+1;
$qaline="$fn\t$fq\t$fa\t\n";
print FILE $qaline;
}
close(FILE);
$chk=0;
}
}
if($chk != 0){erreur($chk);}
print"</FORM></BODY></HTML>\n";
}



 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-08-2003
(E-Mail Removed) (Tad McClellan) writes:

> Guy <(E-Mail Removed)> wrote:


> > @qafield=();
> > push(@qafield,split(/\t/,$qaline));
> > $number=$qafield[0];

>
>
> You can replace those 3 lines with one line that uses a "list slice":
>
> my $number = (split(/\t/,$qaline))[0]; # untested


Or, better still, a list assignment:

my ($number) = split(/\t/,$qaline); # untested

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Walt Mankowski
Guest
Posts: n/a
 
      12-10-2003
On 2003-12-07, Guy <(E-Mail Removed)> wrote:
> Well I think I found the cause, but I still don't understand what's wrong...
> (BTW, I will read up on perldoc -q quot, thanks for that
>
> I recently added a locking scheme to prevent 2 instances of my Perl script
> from updating the text data files at the same time.
>
> Basically it locks a dummy file for exclusive - once the file is locked it
> does its stuff - then it closes that dummy file.
>
> In any case, it turns out that if I don't call those locksys and unlocksys
> routines of mine, this problem doesn't occur.
> Am I doing anything wrong.
> The only thing I can see different is that I have 2 files open at the same
> time: LCKFILE and FILE
> Do I need to so anything before opening a 2nd file or using a 2nd file
> handle?


This code looks fine as well. I think the locking here is a symptom, not the
cause of your problem. My theory remains that somehow your Perl script is being
called twice. If you have locking, then they're effectively single-threaded
and update your file one after another. Without locking they both run
simultaneously and whichever process happens to finish last overwrites the work
of the one that finished first.

Have you checked your weblogs yet to see if the perl script is being hit twice?

Walt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Safari 2.0.0.4 appends .html to pdf files Gerrit Horeis ASP .Net 1 05-30-2007 05:28 PM
Forcing Download of File Appends HTML from Download Page to Downloaded File? Brett Kelly ASP .Net 1 06-16-2006 04:05 AM
File upload/download from database. Download appends aspx page to end of file Ryan Taylor ASP .Net 2 11-10-2004 08:31 PM
Email AND File Appends dmiller23462 ASP General 1 05-28-2004 05:26 PM
Response.redirect appends Port number to URL Sanjay Patil ASP .Net 0 05-10-2004 03:04 PM



Advertisments