In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to format the output of Perl", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to format the output of Perl" this article.
Perl formatted output
I. print format
1. Set the system variable $~ to the format you want to use
2. Call the function write
If you do not specify the print format without $~, the Perl interpreter assumes that the format name to be used is the same as the file variable to be written.
Second, display values in print format
The main reason we use the print format, of course, is to format values stored in simple variables or array variables to produce readable output, which is achieved in a "range". Each range specifies a value, such as a variable or expression, that is displayed in the format specified by the range when the write function is called.
Local variables defined using my require that the format be defined within the subroutine, otherwise they will not be output, so local variables output with write must be defined in local.
Range format
@ right-aligned output
@ | align the output in the
@ # #. # # fixed precision number
@ * Multiline text
The * * characters in each range are line fillers, and when the @ character is used, the text is not formatted.
In the print format, a specific character, such as @, is regarded as a range definition, and the character itself needs to be output in the "@" way.
Third, export to other files
By default, the function write outputs the results to the standard output file STDOUT, or we can make it output to any other file. The easiest way is to pass the file variable to write as a parameter, such as:
Write (MYFILE)
In this way, write is output to the file MYFILE in the default print format called MYFILE, but you cannot use the $~ variable to change the print format used. The system variable $~ only works on the default file variable. We can change the default file variable, change $~, and then call write.
When select changes the default file variable, it returns the internal representation of the current default file variable so that we can create subroutines and output as we like without affecting the rest of the program.
IV. Pagination
When you output to the printer, you can output the corresponding information at the top of each page, such a special text is called a header. Defining a header is actually defining a print format called filename_TOP.
A range of values can also be included in the definition of the header, and the current page number is stored in the system variable $%.
We can also change the print format name that defines the header by changing the system variable $^. Like $~, $^ only works on the current default file, so it can be used in conjunction with the select function.
By default, each page is 60 lines long, and you can change the page length by changing $=, which must appear before * write statements.
Note: the print function is generally not used when using the paging mechanism, because when using write output, the Perl interpreter tracks the current line number of each page. If you must use print without disrupting the page count, you can adjust the system variable $-. $- means the number of lines between the current line and the end of the page, and when $- reaches 00:00, a new page starts.
5. Format long strings
We have learned that the range @ * can output multiple lines of text, but it completely outputs the string as is without formatting.
The definition of a range for formatting long strings (including line breaks) in Perl is as simple as replacing the @ character with ^. In this text format, the Perl interpreter places as many words on one line as possible. Each time a line of text is output, the exported substring is removed from the variable, and the variable is used again in the field value to continue to output the remaining string in format. When the content has been output, the variable becomes an empty string, and then the output will output a blank line. To avoid outputting a blank line, you can add a ~ character at the beginning of the range format line.
When the length of the string is not clear, add two ~ characters to the first line of the field value format, so that the text will continue to be output in format until the output is complete.
Format the output with printf
Printf is basically the same as printf in the C language. Printf has two parameters, one is a string, which contains one or more field values, and the other is that the variable values corresponding to each field value are replaced in a certain format.
The various forms of field values are as follows:
% c single character
% d decimal integer
% e floating point numbers in the form of scientific counting
% f normal form (fixed point) floating point number
% g Compact form floating point number
% o octal integer
% s string
% u unsigned integer
% x hexadecimal integer
Some of the details are as follows:
1. In the format d, o, u or x, if the integer value is or may be large, you can add an l character, meaning a long integer, such as% ld.
2. The minimum width of the field is indicated by adding a positive integer after the% character. If the width of the output result is not enough, it is aligned to the right, followed by a space. If the positive integer starts with the number 0, the complement character is 0. If the% character is followed by a negative integer, the result is aligned to the right.
3. The width before and after the decimal point can be specified in the floating point field values (% c,% f and% g). For example,% 8.3f means the total width is 8 characters, after the decimal point (that is, the decimal part) is 3 characters, and the extra decimal part is rounded.
4. Use the decimal form n.m above in the range of integers, characters or strings. The integer part n is the total width, and the decimal part m is the * width of the output result. This ensures that there are at least nMum spaces before the output result.
The above is all the content of the article "how to format the output of Perl". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.