Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
There's more than one way to do things
 
PerlMonks  

Re: Need help with searching flatfile and updating it.

by chas (Priest)
on Apr 24, 2005 at 22:14 UTC ( [id://451077]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Need help with searching flatfile and updating it.

It isn't at all clear to me that your splitting on '&' of $buffer or $ENV{'QUERY_STRING'} (and your further decoding) is going to get you the exact data you need. It may...I'm not claiming it definitely won't, but life would be much easier if you use CGI.pm and the "param" method to get the data you need. You won't have to worry about GET or POST methods and the decoding of the encoded data will be done automatically for you. If you are going to use Perl, make use of one of the most useful modules available; you'll be glad you did!
chas
  • Comment on Re: Need help with searching flatfile and updating it.

Replies are listed 'Best First'.
Re^2: Need help with searching flatfile and updating it.
by Anonymous Monk on Apr 24, 2005 at 23:06 UTC
    Thanks for the critisism about cgi.pm, but I really Need help with searching flatfile and updating it, not cgi.pm. Now I have some new code here that maybe you could help me figure out why the script always clobbers and not ammends the file if the $browser is $not_found.

    My form:

    <FORM ACTION="cgi-bin/basic3.pl" METHOD="POST"> <SELECT NAME="browser"> <OPTION VALUE="microsoft">microsoft</OPTION> <OPTION VALUE="netscape">netscape</OPTION> <OPTION VALUE="aol">aol</OPTION> <OPTION VALUE="mozilla">mozilla</OPTION> <OPTION VALUE="opera">opera</OPTION> </SELECT> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM>
    My script:
    #!/perl/bin/perl use CGI; $cgi = new CGI; for $key ( $cgi->param() ) { $form{$key} = $cgi->param($key); } $logpath = "data/browser.dat"; $browser = $form{'browser'}; open (LOG, "$logpath"); @data = <LOG>; close(LOG); $not_found = 1; open (LOG, ">$logpath"); foreach $line(@data) { chomp($line); ($fcount,$fbrowser)= split(/\|/,$line); if ($fbrowser eq $browser) { $not_found = 0; $fcount = $fcount+1; print LOG "$fcount|$fbrowser\n"; } else { print "$line"; } } close(LOG); if($not_found) { open (LOG,">>$logpath"); print LOG "1|$browser"; close(LOG); } # prints out log to browser print "Content-type: text/html\n\n"; open (LOG, "$logpath"); @data = <LOG>; close(LOG); foreach $line(@data) { chomp($line); print "$line\n"; } close(LOG); print "\n\n$not_found";
      Can you explain what you're trying to do? It's a bit baffling. Can you also show us an example of your log file?


      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print
        Can you read?
        I believe the poster is merely trying to increment a database containing the fields of the select options based on the number of submissions of a given value. I don't understand the poster's term "flatfile" (probably a reference that the database is stored as a file), but given the above understanding, I must second the use of CGI.
      Fletch advised (wisely!) that you always check that the calls to open succeeded. Is it possible that the file "data/browser.dat" is never actually opened and that's why you never see it amended? In fact, usually the path has to be given in some suitable way relative to the top directory that the web server recognizes, and you may not have that right. (It's happened to me!)
      chas
      (Update: Checking that open succeeded might be done by writing an error msg to the browser if the open fails.)
      Just playing with a minimal case, I think I've grasped what you're trying to do -- are you trying to put the lines back into the LOG file which don't match? Put them back unchanged?

      In that case, you probably want to change

      else { print "$line"; }
      to
      else { print LOG "$line\n"; }


      ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
      =~y~b-v~a-z~s; print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://451077]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.