Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Padding strings for a flat file

by Tyke (Pilgrim)
on Mar 27, 2001 at 12:43 UTC ( #67461=note: print w/ replies, xml ) Need Help??


in reply to Padding strings for a flat file

An approach I've found useful to construct records is to define a template line consisting of spaces (and any data that doesn't change from record to record), and then insert the fields using substr. This way you don't need to worry about padding.

#!perl -w use strict; my $template = ' ' x 60; sub insert { my (undef, $field, $position) = @_; substr($_[0], $position, length($field)) = $field; } my $record = $template; insert($record, 'abc', 0); insert($record, 'ghi', 6); print "[$record]\n";
Thanks to Dominus for reminding me of the argument changing trick used by insert to change the record.


Comment on Re: Padding strings for a flat file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-12-22 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (128 votes), past polls