I have an obnoxious online form that gathers upto ~150 user-supplied values. I need to process these values and build a fixed-length record from them. I've been thinking of half a dozen ways to deal with the data, so I thought I'd ask some wise monks to add to the list of how to do handle them.

I've not worked with creating fixed length records before, but I realize that sprintf() is going to be my friend. What I'm trying to figure is a decently efficient way to handle the fact that many of the fields of the record may be submitted empty, which is fine, but I have to make sure they're holding the right amount of spaces in the final record.

Should I declare all the variables up front something like
my $zip_1 = "            "; #12 spaces etc? and then have the variable values updated when I get the values from the form if there is a submitted value for that form?

I have several groups of variables that all need to be the same lengths. 35 is a popular length. After I read in the data from the form, say
$addressee_1 = param('addressee1'); and so on and so forth... can I assign all the variables that need to be 35 characters in length to an array and then foreach the whole array with a $foo = sprintf("%-35s", $foo);? Or are those variable changes local to the array? What I mean, is can I reference those variables later just as $addressee_1 and get the variable with the change, or do I have to reference them as part of the array (my array syntax sucks and I can't think of it at the moment) to get the padded/truncated value?

Vague enough? I welcome any questions to help me clarify my thinking. Thanks!

----------------------------------------------------------------
I learn more and more about less and less until eventually I know everything about nothing.

In reply to Best way(s) to process form data into fixed-length values? by hmbscully

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.