Sorry, but after reading this i was curious to find a non-regex way. TIMTOWTDI :)use strict; print pad($_,5), "\n" for (qw(3.14 1 10 100 1000)); sub pad { my ($str,$pad) = @_; my ($add,$right); ($str,$right) = split('\.',$str,2); $right = ($right) ? ".$right" : ''; $add = $pad - length($str); return $str . $right if $add < 1; return ('0' x $add) . $str . $right; }
UPDATE: Woah! I made a HUGE mistake! Thanks to Albannach for pointing it out. Code has been modified:
That was dumb. As a matter of fact, trying to get my code to right zero pad decimals (3.1400), instead of left padding (003.14) as it currently does - and for the record, that is NOT A BUG! it is a feature! - well, that's just dumb. i am going to stick with sprintf. :)old: return $str if $add < 1; new: return $str . $right if $add < 1;
and don't chastise me about scientific notation, davorg! ;)
(for the record - for 'some reason' i was thinking the problem was to merely zero pad numbers - oi vey!)
jeffa
just say no to testing your new installation of cygwin on Win32 by trying to simulate a small subset of sprintf in Perl and posting it on PerlMonks
In reply to (jeffa) Re: Using Regular Expressions to Simulate sprintf() function - One Liner Challenge
by jeffa
in thread Using Regular Expressions to Simulate sprintf() function - One Liner Challenge
by Incognito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |