in reply to Leading Zeros

Hmm.. ok. This'll do for what I need right now, but I was sort of looking for a way to use any sort of leading symbol, such as a space or dash. I'm pretty sure you can't do that with sprintf.

Thanks for those two answers anyway.

Replies are listed 'Best First'.
(tye)Re: Leading Zeros
by tye (Sage) on Feb 22, 2001 at 11:05 UTC
    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")
      Thanks. That's just what I was looking for.