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

First off, fixed length is the requirement of the client, I have no say in that. yay me.
I don't have muliple successive entries that need to be the same length. The fields are of various lengths scattered throughout the file layout. My badly-formed original question was looking to see if I could

push @lines35char, $value1; push @lines35char, $value13; push @lines35char, $value 22; sprintf "%-35.35s", $_ for @lines35char;
and then have those padded values show up when I build the line, like so:
$fl_line = join '', $filetype, $batch, $serial, $sys_time, $value1, $t_lastname, $t_firstname, $t_mi, $t_mi, $ssn, $ssn_cor, $dob, $t_address, $t_city, $t_state, $t_zip, $value13, $t_country, $code_1, $date_1, $type_1, $priority_1, $addressee_1, $institution_1,$value22;
or do I have to reference them as they are related to the array?
Thanks!
I learn more and more about less and less until eventually I know everything about nothing.

Replies are listed 'Best First'.
Re^3: Best way(s) to process form data into fixed-length values?
by GrandFather (Saint) on Jul 25, 2006 at 21:49 UTC

    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
      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
Re^3: Best way(s) to process form data into fixed-length values?
by Anonymous Monk on Jul 26, 2006 at 00:02 UTC
    Why would you go to the trouble of pre-padding them? I'm pretty sure you'd be better off performance-wise to just call sprintf once (if that's any sort of concern).

    Assuming an example where odd fields are 5 chars long and evens 15:

    my $var1, $var2, $var3, $var4, $var5; #hopefully you'll be using some +meaningful names instead my $format = "%5s%15s%5s%15s%5s\n"; #you could embed spaces in the for +mat specifier too if needed #check/process inputs here #.... $outputline = sprintf $format, $var1, $var2, $var3, $var4, $var5;
    See the sprintf documentation (pg 797 of Programming Perl 3rd e) for other formats if you need to deal with formatting numerics.

    You can break the format statement up or use repeat specifications too so you don't lose count:

    my $format = "%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s". "%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s". "%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s"; my $otherformat" = "%5s"x40;
    (Though the former is likely to be unwieldy when the client changes the content three times a day)

      Note that %5s will not truncate a string that is too wide. You need %5.5s to do that.


      DWIM is Perl's answer to Gödel