in reply to printf question
You can answer questions like this simply by reading the documentation. The perldoc command makes it pretty easy to find information on standard perl functions using the command "perldoc -f functionName". For example, the documentation on printf would have pointed you to the answer:
[06:28]$ perldoc -f printf printf FILEHANDLE FORMAT, LIST printf FORMAT, LIST Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)", + except that "$\" (the output record separator) is not appended +. The first argument of the list will be interpreted as the " +printf" format. See "sprintf" for an explanation of the format argument. If "use locale" is in effect, and POSIX::set +locale() has been called, the character used for the decimal sep +arator in formatted floating point numbers is affected by the LC_NUMERIC locale. See perllocale and POSIX. Don’t fall into the trap of using a "printf" when a sim +ple "print" would do. The "print" is more efficient and le +ss error prone. [06:28]$ perldoc -f sprintf sprintf FORMAT, LIST Returns a string formatted by the usual "printf" conven +tions of the C library function "sprintf". See below for more d +etails and see sprintf(3) or printf(3) on your system for an explanation of the general principles. For example: # Format number with up to 8 leading zeroes $result = sprintf("%08d", $number); <<<quite a bit of text snipped out>>> (minimum) width Arguments are usually formatted to be only as wide +as required to display the given value. You can overri +de the width by putting a number here, or get the width fr +om the next argument (with "*") or from a specified argume +nt (with e.g. "*2$"): printf '<%s>', "a"; # prints "<a>" printf '<%6s>', "a"; # prints "< a>" printf '<%*s>', 6, "a"; # prints "< a>" printf '<%*2$s>', "a", 6; # prints "< a>" printf '<%2s>', "long"; # prints "<long>" (does + not truncate) If a field width obtained through "*" is negative, +it has the same effect as the "−" flag: left-justifi +cation.
An added benefit of reading the documentation would be that you'd learn quite a bit more about printf/sprintf, which couldn't hurt. If you want to look over all the standard functions (which I do every now and then) you can use "perldoc perlfunc". Be sure to explore all the documentation to find out what's out there so you can answer things yourself.
...roboticus
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: printf question
by Anonymous Monk on Jan 21, 2010 at 13:48 UTC |