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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re^3: Add Null values to my FlatFile by graff
in thread Add Null values to my FlatFile by lakeTrout

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found