in reply to Padding out strings.

My preferred method: #!/usr/bin/perl # padding a string with leading zeros # - $num is the number to be padded # - $pad is the total number of digits you want in the result $num = "345"; $pad = 5; $paddednum = &pad_num($num, $pad); print "num is $num, padded num is $paddednum\n"; exit(0); ############################## sub pad_num { # how it works: # # "0"x$pad yields ($pad) zeros, i.e. "0" x 5 yields "00000" ... # this is prepended to $num, yielding a string with ($pad) zeros # and the number. This becomes the first arg to the substr fn # # ($pad * -1) is the second arg to substr ... when 2nd arg to # substr is negative, substr counts backward from the end of the string # # $pad is the third arg to substr # # thus if $pad is 5 and $num is 345, # result is substr( "00000345", -5, 5 ) or "00345" my ($num, $pad) = @_; my($paddednum) = substr( (("0"x$pad).$num), ($pad*-1), $pad ); return $paddednum; }