aconite has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, As I am a neophyte, I am having some trouble hacking a script to do what I want. I am using Kendall Comey's fom_rocessor.pl to gather input from a form. It is composed of mostly radio button selections. The problem is that if a selection is not made for any given question, it is ignored in the script. The script only outputs to my flat file those key/values (collected in @sortedkeys) that are not null. I want to put a comma in place of any answer that was left blank. The code snippet I am working with is below. Thanks for any input (or is that output <g>)
sub write_form_info { open (FORM, ">>$form_info_path") || die ("I am sorry, but I was unable + to open the file $form_info_path"); foreach $sortedkeys (@sortedkeys) { $sortedkeys =~ s/^\d*\)\s*//; $sortedkeys =~ s/required-//; ($name, $answer) = split (/\|/, $sortedkeys); print FORM "$name -- $answer\n"; } print FORM "\n"; close (FORM); }

Replies are listed 'Best First'.
Re: Key/Value Problem
by arturo (Vicar) on Apr 19, 2001 at 19:12 UTC

    Hmm, I looked KC's script up and see that it contains some really old code:

    #Require the cgi library require './cgi-lib.pl'; #Turn off Perl buffering $| = 1; &ReadParse(*input);

    Nowadays, you should be using CGI to process form data as Chady suggests; one good reason is that CGI is still in active development (which I doubt is true of cgi_lib.pl, which I have heard, but cannot personally confirm, has security issues).

    Looking over the documentation for the code, it doesn't do anything that isn't already fairly simple with CGI; and since that's the industry standard, you have at least the "everybody else uses it because it's good code" reason to use it instead of this script.

    I'd be wary of using stuff I found on http://www.freecode.com if this is a representative sample! Not that it's *wrong* per se, just really outdated (if you're learning to code, you should be using today's tools).

    HTH perl -e 'print "How sweet does a rose smell? "; chomp $n = <STDIN>; $rose = "smells sweet to degree $n"; *other_name = *rose; print "$other_name\n"'

Re: Key/Value Problem
by suaveant (Parson) on Apr 19, 2001 at 18:10 UTC
    The problem is that if an item is not selected in a form, it is not sent through to the script, so the script has no way of knowing what it is missing. I would add a list of the fields to your script, and go through them, and put the value in from the form if it is there, and otherwise put just put a comma like you want.
                    - Ant
      That makes sense to me, and I appreciate the input. Can you show me an example of how I would add a field list and then iterate through them, presumably with a foreach loop. Thanks
        Sure... make an array of names...
        @fields = ('required-name','age'); # etc etc etc foreach $sortedkeys (@sortedkeys) { $sortedkeys =~ s/^\d*\)\s*//; $sortedkeys =~ s/required-//; ($name, $answer) = split (/\|/, $sortedkeys); $form{$name} = $answer; } #so now all your data is in a hash, keyed off the name. foreach $field (@fields) { #check if form value exists, if yes, put form value in #$answer, otherwise put a comma? $answer = (exists $form{$field} ? $form{$field} : ','); print FORM "$field -- $answer\n"; }
        That make sense?
        They will print in the order you put @fields in.
                        - Ant
Re: Key/Value Problem
by Chady (Priest) on Apr 19, 2001 at 18:41 UTC

    Why aren't you using CGI.pm ??

    try to use it and when you do, try this :

    $query = new CGI; $name = $query->param('name') ? $query->param('name') : ','; $answer = $query->param('answer') ? $query->param('answer') : ',';

    I hope this works anyway..


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.