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

There is no need to init variables with padded strings. Consider:

my $zip_1 = ''; #... printf ">%12.12s<\n", $zip; printf ">%12.12s<\n", '123456789012';

Prints:

> < >123456789012<

To manage multiple successive entries that ought be the same length:

my @lines; push @lines, 'Address line 1'; push @lines, ''; # Second line of address skipped push @lines, 'no zip stupid form'; push @lines, 'My beloved country'; push @lines, 'The %35.35s syntax will truncate this is a long line.'; printf "%-35.35s\n", $_ for @lines;

Prints:

Address line 1 no zip stupid form My beloved country The %35.35s syntax will truncate th

But why fixed length records rather than CSV, XML or using a database?


DWIM is Perl's answer to Gödel

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

    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.

      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

      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