Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Add Null values to my FlatFile

by snopal (Pilgrim)
on Nov 26, 2007 at 21:37 UTC ( #653093=note: print w/replies, xml ) Need Help??

in reply to Add Null values to my FlatFile

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.

Replies are listed 'Best First'.
Re^2: Add Null values to my FlatFile
by lakeTrout (Scribe) on Nov 26, 2007 at 21:46 UTC
    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://653093]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2022-01-17 15:24 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.