in reply to Re: Leading Zeros
in thread Leading Zeros
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")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: Leading Zeros
by Monolith-0 (Beadle) on Feb 22, 2001 at 11:13 UTC |