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

hi Folks,

I have built a form that dumps everything into a flatfile. I'm trying to clean things up a bit and use better practices.

My problem stems from when I have empty fields (for example checkboxes that are left un-checked etc), they are not being added to the flatfile as a "blank" tab. Can anyone see where I'm going wrong here? Is it the sort that's kicking out my "null" values?
sub makelog { open (LOGFILE, ">>$logfile") or die("Can't Open Log File at $l +ogfile"); @FieldNames = sort (keys %FORM); foreach $key (@FieldNames) { print LOGFILE ("\t"."$FORM{$key}"); } print LOGFILE ("\n"); close (LOGFILE); }
From a rusty perl novice, Thanks!

LakeTrout

Update: It appears that if I add a new line in a text area,things start to go wrong from there. I added this to my split function:
    $value =~ s/%0D%0A/\, /g;
that didn't seem to help either. Here's a snippet:
@pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ s/%0D%0A/\, /g; ... }

Replies are listed 'Best First'.
Re: Add Null values to my FlatFile
by snopal (Pilgrim) on Nov 26, 2007 at 21:37 UTC

    Taking a stab at this...

    I assume that %FORM is key/value pairs of the returned values from the form. Unfortunately, if a value is not set, it is not passed, and very likely will not have a key/value pair.

    Your best avenue is to create your own @param_list of 'must have keys' and determine if they are not present. If they are not, then you need to have code determine the best default value for those unpassed parameters.

      Thanks snopal,

      I see what you mean, and that would be fine if I had required fields for some and not others. I do have validation in there for some, however, it's the non-required checkboxes that are giving me the most fits. I'm a rusty perl novice, so please bare with me. Is there a way I can test the key for a null and just print a tab (\t) somewhere in the foreach, if so, do you have a suggestion?

      Thank you!
        I think the point is: the form that produces your input has a fixed number of possible fields, and you should know what all these fields are (at least, what they are called), because you would see all those field names whenever the form is filled in completely with nothing left blank.

        Your problem arises when some fields are not filled in, so their names don't show up at all in the form submission -- you have to know what they are in advance of receiving a given submission, so that you can figure out when they are missing.

        So, given that there is a finite, maximum set of fields as defined by the complete form, you should change this line in the OP code:

        @FieldNames = sort (keys %FORM);
        to be something like this:
        @FieldNames = qw/Fieldname1 Fieldname2 Fieldname3 .../;
        where each "FieldnameN" represents an actual name of a parameter key coming from the form, the array contains the complete set, and the names are in the desired order for printing to the log file.

        Then your loop over the key names would go something like this:

        foreach $key (@FieldNames) { my $val = ( defined( $FORM{$key} ) and $FORM{$key} =~ +/\S/ ) ? $FORM{$key} : "BLANK"; print LOGFILE "\t$val"; }
        though personally I prefer concatenating fields into a line string, and printing the line:
        my $line = ''; foreach $key (@FieldNames) { $line .= "\t" . ( defined( $FORM{$key} ) and $FORM{$ke +y} =~ /\S/ ) ? $FORM{$key} : "BLANK": } print LOGFILE "$line\n";
        Note that the "defined()" check is simply there to avoid warnings about string operations on undefined values, in case you happen to have "-w" or "use warnings" in your script (which is generally a good idea). Then the regex match allow any value containing non-whitespace to get into the log file.
Re: Add Null values to my FlatFile
by apl (Monsignor) on Nov 26, 2007 at 21:14 UTC
    You might show what the contents of %FORM (and the output to LOGFILE) look like.

    Failing that, I'd suggest you  print LOGFILE "/$key/".$FORM{$key}."\n"; instead of your current print, to see what is going on.
      Hi, thanks for taking a look. The log file prints everything in sequence, something like this:
      Head1 Head2 Head3 Head4 Head5 val1 val2 val9 val10 val1
      what I want is this:
      Head1 Head2 Head3 Head4 Head5 ... Head9 val1 val2 val9
      where values 3-8 are null, they don't print. I'll try you suggestion and see what that gives me.

      Thank you!

      UPDATE:
      adding: print LOGFILE "/$key/".$FORM{$key}."\n"; instead of what I have already doesn't add the fields with no value, it just prints element on a new line, like it's a new record in the flat file
        I was vague; my apologies. I suggested the new print as a debugging aid. It wasn't intended as a solution to the problem.

        Having seen the follow-up, I'd agree with aquarium; if the value is NULL, force it to a blank or a fixed string.
Re: Add Null values to my FlatFile
by aquarium (Curate) on Nov 26, 2007 at 21:58 UTC
    would this do?
    print LOGFILE ("\t"."$FORM{$key}" or "BLANK");
    the hardest line to type correctly is: stty erase ^H
      Thanks aquarium,

      I tried that, but sadly it didn't work. I think I had an epiphany, I'm going to update my original note. It appears when I add \n (line feeds, whatever) to my textareas, things go wrong from there.
        then a bit of additional code can still make the short-circuit OR work, e.g.
        print LOGFILE ("\t".($FORM{$key}=~/[a-zA-Z0-9])?"$FORM{$key}:"BLANK")) +;
        untested...you get the idea
        the hardest line to type correctly is: stty erase ^H