sub pad {
my( $str, $width, $delim, $pad )= @_;
$width= 3 if ! defined $width;
$delim= "." if ! defined $delim;
$pad= "0" if ! defined $pad;
return join $delim, grep {s/ /$pad/g;1}
map {sprintf"%${width}s",$_}
split /\Q$delim\E/, $str, -1;
}
Use a negative width to pad in the other direction.
Update: Fixed a minor bug (my abuse of grep should have ended with ";1" not ";$_") and wanted to note that the transformation of " " to $pad when it is part of the original string but not part of a delimiter is a feature. ;)
-
tye
(but my friends call me "Tye") |