![]() O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers. ![]() Get Learning Perl, Fourth Edition now with the O’Reilly learning platform. The Perl printf is much like the printf function in C and awk in that it takes a string to be formatted and a list of format arguments, applies the formatting to the string, and then typically prints the formatted string to standard output, which in our case, is the Web browser. The %d format means a decimal integer, truncated as needed: printf "in %d days!\n", 17.85 # in 17 days! To print a number, generally use %g, which automatically chooses floating-point, integer, or even exponential notation as needed: printf "%g %g %g\n", 5/2, 51/17, 51 ** 17 # 2.5 3 1.0683e 29 To change the cases of a string you use a pair of functions lc () and uc () that returns the lower case and upper case versions of a string. Of course, the full details are available in the perlfunc manpage. There are many possible printf conversions, so we’ll take time here to describe the most common ones. Syntax: printf FILEHANDLE FORMAT, LIST printf FORMAT. This function is used to have a formatted output from a list. The example above has two items and two conversions, so the output might look something like this: Hello, merlyn your password expires in 3 days! What is printf Function in Perl Explanation. (As we’ll see in a moment, there may be significant extra characters between these two symbols.) There should be the same number of items in the following list as there are conversions if these don’t match up, it won’t work correctly. The format string holds a number of so-called conversions each conversion begins with a percent sign ( %) and ends with a letter. The format string is a fill-in-the-blanks template showing the desired form of the output: printf "Hello, %s your password expires in %d days!\n", The printf operator takes a format string followed by a list of things to print. Comments to Ask Bjrn Hansen at Group listing About. : Perl Programming lists via nntp and http. Re: printf and zero padding by Harry Putnam. Re: printf and zero padding by Jim Gibson. Fear not-Perl provides a comparable operation with the same name. Prints a formatted string of the elements in list to filehandle or, if omitted, the currently selected output. I seem to forget about 90 of what I know between scripts. printf '05dn', 30 This will include leading zeros in front of number 30 making a total count of digits to 5. In fact, you may be accustomed to the formatted output of C’s printf function. A simple format can be written using printf or sprintf functions available in Perl. You may wish to have a little more control with your output than print provides.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |