Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Format, empty vars, and fixed-width records by kwaping

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found