Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Padding strings for a flat file

by Anonymous Monk
on Mar 27, 2001 at 03:43 UTC ( [id://67382]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a flat file and need to print some values, padding them out to various field lengths. I'd like to know how to pad with spaces and how to pad with x's.

Thanks for any help, and pardon the extreme newbiness.

Replies are listed 'Best First'.
Re: Padding strings for a flat file
by ar0n (Priest) on Mar 27, 2001 at 03:48 UTC
    sub pad { my $str = shift; my $maxlen = shift; my $char = shift || " "; return $char x ($maxlen - length($str)) . $str; } print pad("Foo Bar", 30, "x"); # prints 'xxxxxxxxxxxxxxxxxxxxxxxFoo Ba +r'


    [ar0n]

      I wonder if this might be a good time to use Perl's bizarre pass-by-reference semantics?
      sub pad { my $char = pop; my $max = pop; $char = " " unless defined $char; $_[0] .= $char x ($max - length $_[0]); }
      Now:
      pad($v, 30, 'x'); # $v is now 30 characters long print $v;
Re: Padding strings for a flat file
by Masem (Monsignor) on Mar 27, 2001 at 03:51 UTC
    Assuming you mean values as in only numerical values, sprintf is your friend.

    sprintf("%10d", 1234) will give you "......1234" (those '.' are spaces, really, but PM formatting weirds it out).

    sprintf("%010d", 1234) will give you "0000001234"

    If you want x's instead, then try this (assuming you only have numerics and no strings)

    $text = sprintf("%10d", $number); $text =~ s/\s/x/g;
    If this doesn't work, there are format statements in perl, but my experience tells me there are rather cumbersome to use.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Padding strings for a flat file
by Tyke (Pilgrim) on Mar 27, 2001 at 12:43 UTC
    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.

Log In?
Username:
Password:

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

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

    No recent polls found