#!/usr/bin/perl -w use strict; =head1 I MISUNDERSTOOD MY INPUT AND FUNCTION Apparently sprintf does not provide maximum length for number types, but only for strings. So in effect i was getting ints wih 8 or 9 digits before the . and the min length was 8, but you can't specify max length. Live and learn, and then make an ass out of yourself (almost) ;-^)~ I ended up making sure my input was an integer, and then treating it as a string with max and min length specified. Basically i feed it to sprintf twice =cut =head1 SPRINTF NOT PADDING CORRECTLY (original question) print sprintf(".crazy.%010u.temp",rand(time)),"\n"; print sprintf(".crazy.%08.8s.temp",rand(time)),"\n"; Its' fine if i use string, but it's supposed to be minimum length for int but it doesn't pad with d (signed int), or with u (unsigned int). unless it's the length is 9 or greater. Is this flaw, or what? If i use 8, some numbers don't get padded. If i use 9 or greater, they do. I don't think this has to do with my specific version of perl, which is ActiveState 5.6(build 623) btw. Why, cause perldoc:pelrfunc says: Perl does its own sprintf formatting--it emulates the C function sprintf, but it doesn't use it (except for floating-point numbers, and even then only the standard modifiers are allowed). As a result, any non-standard extensions in your local sprintf are not available from Perl. And since i'm not trying to represent a floating point number (i want int), it should make no difference(not that it would anyway). Also, I do not wish to use %08s (for string), because sometimes a . sneaks in, which is because rand(time) outputs a positive float that looks like BTW - rand(time) yields results like: 598318636.889648 52034455.75 52445775.5732422 However, it hardly yields a value less than 8 digits digits before the decimal, but that's besides the point. BTW - srand is called automatically in my version of perl, so i don't call it explicitly cause it makes my results duplicates 1/3 of the time. =cut for(0..99) { print ' rand( ', rand(time),"\n"; } =head1 WHAT I TRIED =pod Here come the un-signed integer examples. sprintf(".crazy.%08u.temp" ,rand(time)) # min length 8, padd w/0 sprintf(".crazy.%08.8u.temp" ,rand(time)) # min length 8 or 8, padd w/0 sprintf(".crazy.%010u.temp" ,rand(time)) # min length 10, padd w/0 sprintf(".crazy.%0.8u.temp" ,rand(time)) # not actually sure It's either padd with 0 w/min length of 8, or BTW - rand(time) yields results like: 598318636.889648 52445775.5732422 =head1 MY SOLUTION sprintf(".crazy.%08.8s.temp", sprintf("%u",rand(time)) ); Feed sprintf an unsigned int, and then feed that unsigned int to a sprintf with 0 for padding and a string of max length 8 and min length 8 (you gotta specify both to get a fixed width) or sprintf(".crazy.%08.8s.temp", int(rand(time)) ); It's a little bit faster, cause it uses int, but it's the same shit. =cut print &_titled_hr(' sprintf(".crazy.%010.10s.temp", sprintf("%u",rand(time))) '); for(0..99) { print ' %010.10s(', sprintf(".crazy.%010.10s.temp", sprintf("%u",rand(time))), "\n"; } print &_titled_hr(' sprintf(".crazy.%010.10s.temp",rand(time)) '); for(0..99) { print ' %010.10s(', sprintf(".crazy.%010.10s.temp", int( rand(time) ) ), "\n"; } print &_titled_hr(' sprintf(".crazy.%08u.temp",rand(time)) '); for(0..99) { print ' %08u(', sprintf(".crazy.%08u.temp",rand(time)),"\n"; } print &_titled_hr(' sprintf(".crazy.%0.8u.temp",rand(time)) '); for(0..99) { print ' %0.8u(', sprintf(".crazy.%0.8u.temp",rand(time)),"\n"; } print &_titled_hr(' sprintf(".crazy.%08.8u.temp",rand(time)) '); for(0..99) { print ' %08.8u(', sprintf(".crazy.%08.8u.temp",rand(time)),"\n"; } print &_titled_hr(' sprintf(".crazy.%010u.temp",rand(time)) '); for(0..99) { print ' %010u(', sprintf(".crazy.%010u.temp",rand(time)),"\n"; } =pod Here come the signed integer examples. sprintf(".crazy.%08d.temp" , rand(time))) sprintf(".crazy.%0.8d.temp" , rand(time))) sprintf(".crazy.%08.8d.temp" , rand(time))) sprintf(".crazy.%010d.temp" , rand(time))) BTW - rand(time) always yields positive floats. =cut print &_titled_hr(' sprintf(".crazy.%08d.temp",rand(time)) '); for(0..99) { print ' %08d(', sprintf(".crazy.%08d.temp",rand(time)), "\n"; } print &_titled_hr(' sprintf(".crazy.%0.8d.temp",rand(time)) '); for(0..99) { print ' %0.8d(', sprintf(".crazy.%0.8d.temp",rand(time)), "\n"; } print &_titled_hr(' sprintf(".crazy.%08.8d.temp",rand(time)) '); for(0..99) { print ' %08.8d(', sprintf(".crazy.%08.8d.temp",rand(time)), "\n"; } print &_titled_hr(' sprintf(".crazy.%010d.temp",rand(time)) '); for(0..99) { print ' %010d(', sprintf(".crazy.%010d.temp",rand(time)), "\n"; } exit; sub _titled_hr { my $string= join('', @_); my $oy = int (80 -(length $string) )/ 2; $oy -=2; return "\n ","-" x $oy, $string, "-" x $oy," \n"; }; ############################################################################## ## END OF SCRIPT __END__