 |
| |
Perl sprintf Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
The Perl sprintf function returns a string formatted by the usual printf conventions of the C library function sprintf. Instead of the printf function which prints the string to the current output filehandle or to the one specified by a filehandle, the sprintf function will return the formatted string in a scalar variable.In this short free tutorial I'll show you a few examples about how to use this function to get a specific formatted string. The syntax form of the Perl sprintf function is as follows: $string = sprintf FORMAT, LIST
where the FORMAT is used to return a formatted string based on the LIST values.The sprintf function FORMAT allows you to use the following conversions:
| Symbol | Meaning, Example | | %% | a percent sign | | %c | a character with the given number
my $string = sprintf '%c', 0x61;
print "$string\n"; # it displays: a
| | %s | a string
my $string = sprintf '%s', "abcd";
print "$string\n"; # it displays: abcd
| | %d | a signed integer, in decimal
my $string = sprintf '%d', 123415;
print "$string\n"; # it displays: 123415
| | %D | a synonym for %ld
my $string = sprintf '%ld', 234151345;
print "$string\n"; # it displays: 234151345
| | %i | a synonym for %d
my $string = sprintf '%i', -17324;
print "$string\n"; # it displays: -17324
| | %u | an unsigned integer, in decimal
my $string = sprintf '%u', 2217324;
print "$string\n"; # it displays: 2217324
| | %U | a synonym for %lu
my $string = sprintf '%U', 2217324334;
print "$string\n"; # it displays: 2217324334
| | %o | an unsigned integer, in octal
my $string = sprintf '%o', 934522;
print "$string\n"; # it displays: 3441172
| | %O | a synonym for %lo | | %x | an unsigned integer, in hexadecimal
my $string = sprintf '%x', 934522;
print "$string\n"; # it displays: e427a
| | %X | like %x, but using uppercase letters
my $string = sprintf '%X', 934522;
print "$string\n"; # it displays: E427A
| | %e | a floating-point number in scientific notation
my $string = sprintf '%e', -12.345;
print "$string\n"; # it displays: -1.234500e+001
| | %E | like %e, but using uppercase "E"
my $string = sprintf '%E', -12.345;
print "$string\n"; # it displays: -1.234500E+001
| | %f | a floating-point number in fixed decimal notation
my $string = sprintf '%f', -12.345;
print "$string\n"; # it displays: -12.345000
| | %F | a synonym for %f
my $string = sprintf '%F', -12.345;
print "$string\n"; # it displays: -12.345000
| | %g | a floating-point number in %e or %f notation
my $string = sprintf '%g', -1.234500E+1;
print "$string\n"; # it displays: -12.345
| | %G | like %g, but with an uppercase "E", if applicable | | %b | an unsigned integer, in binary
my $string = sprintf '%b', 123;
print "$string\n"; # it displays: 1111011
| | %B | like %b, but using an uppercase "E", with the # flag | | %p | a pointer (outputs the Perl value’s address in hexadecimal) | | %n | special: *stores* the number of characters output so far into the next variable in the parameter list |
Check my new How To Tutorial eBook (PDF format):to see a lot of fully commented examples that help you use the sprintf function in your scripts.
You may specify several additional attributes between the % and the format letter in order to control the interpretation of the Perl sprintf function format:| Attribute | Meaning, Example | format parameter index | By default sprintf will format the next unused argument in the list, but this parameter index, such as 2$, enables you to take the arguments out of order.
my $string = sprintf '%2$d %1$d', 12, 34;
print "$string\n"; # it displays: 34 12
$string = sprintf '%3$d %d %1$d', 1, 2, 3;
print "$string\n"; # it displays: 3 1 1
| flags one or more of:space + - 0 #
|
prefix non-negative number with a space prefix non-negative number with a plus sign left-justify within the field use zeros, not spaces, to right-justify ensure the leading "0" for any octal, prefix non-zero hexadecimal with "0x" or "0X", prefix non-zero binary with "0b" or "0B" | | vector flag | It tells Perl to interpret the supplied string as a vector of integers, one for each character in the string.
Perl applies the format to each integer in turn, then joins the resulting string with a separator (a dot . by default).
If you put an asterisk * before the v you can override the string to use to separate the numbers:
my $string = sprintf "%*vd", ":", "AB\x{100}";
print "$string\n"; # it displays 65:66:256
| | (minimum) width | Arguments are usually formatted to be as wide as required todisplay the given value, but you can override the width by putting a number here, or get the width from the next argument (with *):
my $string = sprintf "%5s", "a";
print "$string\n"; # it displays " a"
$string = sprintf "%*s", 5, "a";
print "$string\n"; # it displays " a"
| | precision, or maximum width | You can specify a precision (for numeric conversions) or a maximum width (for string conversions) by specifying a dot . followed by a number.
For floating-point formats (excepting g or G) this specifies how many places right of the decimal point to show:
my $string = sprintf "%.2f", 123.4567;
print "$string\n"; # it displays: 123.46
$string = sprintf "%.5s", "language";
print "$string\n"; # it displays: langu
| | size | For numeric conversions, you can specify the size to interpret thenumber as using l, h, V, q, L or ll. So: l - interpret integer as C type "long" or "unsigned long" h - interpret integer as C type "short" or "unsigned short" or quads (typically 64-bit integers) q, L or ll - interpret integer as C type "long long", "unsigned long long" or "quads" (typically 64-bit integers) | | order of arguments | You can use * to require additional arguments, these are consumed from the argument list in the order they appear in the format specification before the value to format:
my $val = 1234.12345;
my $string = sprintf "%*.*s", 9, 7, $val;
print "$string\n"; # it displays: " 1234.12"
In the above example, 9 is uses for the width, 7 for the precision, and $val as the value to format. |
Du you want to see more examples? Check my new "Perl Scalar and String Functions - How To Tutorial eBook" to see other examples about how to use the Perl sprintf function in your scripts:- How to use the Perl sprintf function to convert from decimal to hexadecimal
- How to use the Perl sprintf function to convert from decimal to octal
- How to use the Perl sprintf function to pad a string with blanks
- How to use the Perl sprintf function to format floating-point numbers
- How to use the Perl sprintf function with regex
- How to use the Perl sprintf function to pad a number on the left with zeroes
- How to use the Perl sprintf function to expand variables in text strings
and much more.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :
Perl How To Tutorial eBooks
Table of Contents:
A Perl Script Install Perl Running Perl Perl Data Types Perl Variables Perl Operators Perl Lists Perl Arrays Array Size Array Length Perl Hashes Perl Statements Perl if Perl unless Perl switch Perl while Perl do-while Perl until Perl do-until Perl for Perl foreach Built-in Perl Functions Functions by Category String Functions List Functions Array Functions Hash Functions Miscellaneous Functions Functions in alphabetical order chomp chop chr crypt defined delete each grep hex index join lc lcfirst length map oct ord pack pop push q qq qw reverse rindex scalar shift sort splice split sprintf substr tr uc ucfirst undef unshift
return from Perl sprintf function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!

|
|