in reply to sprintf zero-fill issue

I usually go with:

my $delta = $desired_length - length $number; while ($delta ne 0) { $number = '0' . $number; --$delta; }
which may not be the most efficient, but I never have to look up sprintf, and neither will anyone who reads the script. This is a consideration for me, since almost nobody else at my shop knows perl, and a lot them don't know sprintf from Unix or C++ either.

Replies are listed 'Best First'.
Re^2: sprintf zero-fill issue
by apl (Monsignor) on Jun 28, 2007 at 14:00 UTC
    I do it similar to you
    my $delta = $maxSize - length( $value ); $value = ( '0'x$delta ).$value if ( $delta > 0 );
    Having said that, I really like the construction Moron described, and plan on trying it. (I'm also the only Perl practioner in my shop. Sadly.)