peokai:

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 "&#8722;" 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


In reply to Re: printf question by roboticus
in thread printf question by peokai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.