appending to file appends twice!?

Discussion in 'Perl Misc' started by Guy, Dec 5, 2003.

  1. Guy

    Guy Guest

    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;
    }
     
    Guy, Dec 5, 2003
    #1
    1. Advertising

  2. Guy <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 6, 2003
    #2
    1. Advertising

  3. On 2003-12-05, Guy <> 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
     
    Walt Mankowski, Dec 6, 2003
    #3
  4. Guy

    Guy Guest

    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";
    }
     
    Guy, Dec 7, 2003
    #4
  5. (Tad McClellan) writes:

    > Guy <> 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\\
     
    Brian McCauley, Dec 8, 2003
    #5
  6. On 2003-12-07, Guy <> 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
     
    Walt Mankowski, Dec 10, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sanjay Patil
    Replies:
    0
    Views:
    773
    Sanjay Patil
    May 10, 2004
  2. Ryan Taylor
    Replies:
    2
    Views:
    985
    Ryan Taylor
    Nov 10, 2004
  3. Brett  Kelly
    Replies:
    1
    Views:
    705
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  4. Gerrit Horeis

    Safari 2.0.0.4 appends .html to pdf files

    Gerrit Horeis, May 29, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    530
  5. dmiller23462

    Email AND File Appends

    dmiller23462, May 27, 2004, in forum: ASP General
    Replies:
    1
    Views:
    130
    Me Padre
    May 28, 2004
Loading...

Share This Page