in reply to Padding strings for a flat file

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]

Replies are listed 'Best First'.
Re: Padding strings for a flat file
by Dominus (Parson) on Mar 27, 2001 at 04:00 UTC
    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;