O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

15.3. Formatting Data with sprintf( )

The printf function is sometimes handy when used to take a list of values and produce an output line that displays the values in controllable ways. The sprintf function is identical to printf for its arguments, but returns whatever would have been output by printf as a single string. (Think of it as "string printf.") For example, to create a string consisting of the letter X followed by a five-digit zero-padded value of $y, it's as easy as this:

$result = sprintf("X%05d",$y);

See the sprintf entry in Chapter 3 of Programming Perl, and the printf (3) manpage (if you have it) for a description of the arguments required by sprintf.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required