in reply to Re^2: Best way(s) to process form data into fixed-length values?
in thread Best way(s) to process form data into fixed-length values?

You could toss the various values into arrays. Something tht may help is:

use warnings; use strict; my @fields; my $fieldValue = 'Grand'; # Given name field contents push @fields, [$fieldValue, 10]; # 20 character wide field $fieldValue = 'Father'; # Sir name push @fields, [$fieldValue, 10]; # 25 character wide field push @fields, ['007 Bond Street', 15]; push @fields, ['Wibble Wobble', 15]; push @fields, ['', 15]; push @fields, ['Erewhon', 15]; #... my $record = join '', map {sprintf "%-*.*s", $_->[1], $_->[1], $_->[0] +} @fields; print $record;

Prints:

Grand Father 007 Bond StreetWibble Wobble Erewh +on

which ties the field size to the field contents at the point where the contents are added to the field list then uses the field size to control field width in a sprintf later on.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^4: Best way(s) to process form data into fixed-length values?
by johngg (Canon) on Jul 25, 2006 at 22:42 UTC
    Minor nit, but I think the two comments on field width (20 and 25 characters wide) should perhaps be 10 characters wide.

    I didn't know you could use asterisks in sprintf formats like that. Neat :)

    Cheers,

    JohnGG

      That's to demonstrate why one should never comment code. :)

      I originally used wider fields, but the line was too long so I narrowed the fields and that invalidated the comments. I'll leave the them like that as a permenent reminder of how not to write comments.


      DWIM is Perl's answer to Gödel