Bootstrap FreeKB - Perl (Scripting) - Append leading zeros to numbers
Perl (Scripting) - Append leading zeros to numbers

Updated:   |  Perl (Scripting) articles

Let's say $var1 contains both single digit and multiple digit numbers.

$var1 = "8 15 4 13 5 4 12";

 

The following will add a leading 0 to the single digit numbers. In this example, %2 means a maximum of 2 characters.

$var1 = sprintf("%2d", $var1);
$var1 =~ tr/ /0/;

 

$var1 should now contain the following.

08
15
04
13
05
04
12

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e53bdc in the box below so that we can be sure you are a human.