Next | Program Repair Shop | 64 |
124 if ($Integer < 10) { 125 return "0000".$Integer; 126 } elsif (($Integer >= 10) and ($Integer < 100)) { 127 return "000".$Integer; ...
The right answer here is to use sprintf:
return sprintf "%05d", $Integer;
10 lines become 1
But what if you don't know about sprintf?
You still should not repeat code
With every feature of the language working for you, there is always a solution:
my $n_zeroes = 5 - length($Integer); my $zeroes = '0' x $n_zeroes; return $zeroes . $Integer;
10 lines become 3
If you don't know about x, you can still use a loop
while (length($Integer) < 5) { $Integer = "0$Integer" }
Some people are fond of:
return substr("00000$Integer", -5);
Next | Copyright © 2002 M. J. Dominus |