Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Format, empty vars, and fixed-width records

by kwaping (Priest)
on Apr 21, 2008 at 22:33 UTC ( #682037=perlquestion: print w/ replies, xml ) Need Help??
kwaping has asked for the wisdom of the Perl Monks concerning the following question:

I need to output data to a fixed-width record file. The lines need to be exactly 129 characters long. I thought format would be great for this, and it has been - except for one issue... The line only prints up to the last variable that contains a non-pad character. My current code runs out of data at position 92, after which everything is undef. That is also where the output line ends. Unfortunately, I need it to pad out to the 129 character requirement.

Can anyone help? Here's what I've tried already:
  • Making the final variable an empty string
  • Making the final variable a single space
  • Removing everything in the format picture after the last data-containing variable and hard-coding a run of spaces
None of those ideas worked - the line was still truncated at 92 characters in each instance.

I also tried using sprintf, but if the data is longer than intended, the line becomes too long. I'd rather not substr and sprintf every piece of data, but I'll do it if that's what's required.

Example code (disregard unintialized and "not a number" warnings):
#!/usr/bin/perl use strict; use warnings; my ( $emp_number, $name, $override_dept, $job, $shift, $d_e, $d_e_code, $override_rate, $hours, $year, $month, $day, $filler1, $filler2, $amount, $seq_num, $override_div, $override_branch, $override_state, $override_local, $state_misc, $rate, $ssn, ) = 'A' .. 'P'; format XXX = @>>>>>@>>>>>>>>>>>>>>>>>>>>>>>>@>>>>>@>>>>>>>>>>>@@@>@#####.##@####.## +@>>>@>@>@>@>@#####.##@@>>>>>@>>>>>@>@>>>>>>>>>@@@>>>>>>>>>> $emp_number,$name,$override_dept,$job,$shift,$d_e,$d_e_code,$override_ +rate,$hours,$year,$month,$day,$filler1,$filler2,$amount,$seq_num,$ove +rride_div,$override_branch,$override_state,$override_local,$state_mis +c,$rate,$ssn . my $record = ''; open my ($out), '>', \$record or die $!; my $oldfh = select($out); $~ = 'XXX'; write; select($oldfh); close($out) or die $!; print $record;


Update: I've decided to go with the substr(sprintf()) method after all. Not the prettiest or most elegant, but it works.

---
It's all fine and dandy until someone has to look at the code.

Comment on Format, empty vars, and fixed-width records
Select or Download Code
Re: Format, empty vars, and fixed-width records
by state-o-dis-array (Hermit) on Apr 21, 2008 at 22:59 UTC
    When I change the line
    ) = 'A' .. 'P';
    to
    ) = 'A' .. 'W';
    it seems to work for me as you are seeking.
      Unfortunately, I can't change the data to make the line print out to character 129. I have to have nothing but pad characters (spaces) from the last real data out to the end of the record.

      ---
      It's all fine and dandy until someone has to look at the code.
        Sorry, that is evident after a more careful reading of the original post.
Re: Format, empty vars, and fixed-width records
by runrig (Abbot) on Apr 21, 2008 at 23:27 UTC
    I would probably use Parse::FixedLength for this...at least for the debugging phase...but I'm biased (since I'm the author). You could just use pack (with "A" for the format types) though.
      Thanks for the suggestions! I'm going to research your proposed solutions. I did see your module when doing a CPAN search, but the naming didn't make it obvious that it could create fixed-length strings as well as read them.

      I have a related suggestion for your module. Please consider creating a new parameter to new called "justify" to ease the creation of records where every field must be right-justified.

      ---
      It's all fine and dandy until someone has to look at the code.
Re: Format, empty vars, and fixed-width records
by BrowserUk (Pope) on Apr 21, 2008 at 23:27 UTC
    I also tried using sprintf, but if the data is longer than intended, the line becomes too long.

    You do know you can specify the maximum width?

    printf "%2.2s>%32.32s<\n", $_, 'x' x $_ for 30 .. 34, 99..101;; 30> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 31> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 32>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 33>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 34>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 99>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 10>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< 10>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you for mentioning that, as I did not know about that detail. However, after reading the perldoc on it, I don't believe that will limit the lengths of my numeric fields, which may be a problem.

      ---
      It's all fine and dandy until someone has to look at the code.

        As I showed with the decimal field at the beginning of the records above, you can truncate integer fields by using %N.Ns. Unfortunately, the truncation always occurs on the right regardless of whether you use '-' or not:

        [0] Perl> printf "%2.2s\n", $_ for 9, 99 .. 101;; 9 99 10 10 [0] Perl> printf "%-2.2s\n", $_ for 9, 99 .. 101;; 9 99 10 10

        Which may not be what you want. The real problem (sic) is floating point numbers, there's just no sensible way to squeeze them into a fixed width field if they stray beyond a limited range:

        printf "%-8.8s\n", sprintf '%8f', $_ for map{ 1.23456789 * $_} map{ "1e$_" } -10 .. 10;; 0.000000 0.000000 0.000000 0.000000 0.000001 0.000012 0.000123 0.001235 0.012346 0.123457 1.234568 12.34567 123.4567 1234.567 12345.67 123456.7 1234567. 12345678 12345678 12345678 12345678

        And its worse if you want or need to maintain point alignment.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://682037]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2014-09-16 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (38 votes), past polls