Perl file operation (2) (suitable for beginners)

zhaozj2021-02-16  49

Read multiple records If you call, return to the remaining records in the file. If you are in the end of the file: @Records =; if (@records) {print "there", scalar (@records), "Records Read.n";} In the following step, assignment and Test two work: if (@Records =) {print "there is", scalar (@records), "Records read.n";} chomp () can also apply to array operations: @records =; chomp (@records For any expression, you can perform a chomp operation, so you can write in the following step: chomp (@records =); what is a record? The default definition of records is: "line". The recorded definition is controlled by the $ / variable, which is stored in the input recorded separator because the linear character (according to the definition!) Is used to separate, so the default is a string "N". For example, you can replace "N" with any symbol you want to replace. $ / = ";"; $ record =; # Read into the next semicolon-separated record $ / can take other two interesting values: empty string ("") and undef.

The reading of paragraph $ / = "" is used to indicate that Perl read paragraphs, and the paragraph is a text block composed of two or more resolve. This is different from the set to "NN", which only reads the text block consisting of two lines. In this case, there will be such a problem: if there is a continuous space, such as "textnnnn", you can explain it as a paragraph ("text"), or interpret it as two paragraphs ("Text" ", Followed by two newline characters, and an empty segment, followed by two blank lines.) When reading the text, the second explanation is not large. If you are reading the paragraph, you don't have to filter out the "empty" paragraph. $ / = "nn"; while () {chomp; next unless length; # 过 空段 # ...} You can set the $ / set to undef, which is used to read the back followed by two or more newline characters Segmentation: undef $ /; while () {chomp; # ...} Reads the other interesting values ​​of the entire file $ / to undef. If set to this value, you will tell Perl, and the read command will return the remainder of the file as a string: undef $ /; $ file =; because the value changed, it will affect each read operation in the future, Not only is the next read operation. Typically, you need to limit this operation to be partial. By the following example, you can read the contents of the file handle into a string: {local $ / = undef; $ file =;} Remember: Perl variables can be read in a long string. Although your file size cannot exceed your virtual memory capacity, you can still read as much data as possible. Opening the file with a regular expression Once you have a variable that contains the entire string, you can use the regular expression to operate the entire file instead of a block in the file. There are two useful regular expressions tags / s and / m. Generally, Perl's regular expression is processed, you can write: undef $ /; $ line =; if ($ line = ~ /(b.*grass $/) {Print "Found $ 1N";} If our files are filled in the following: Browngrass Bluegrass is output to: Found Bluegrass It didn't find "browngrass" because $ is only looking for match, (or a line before the string). If in the string containing a lot of rows, use "^" and "$" to match, we can use / M ("multiline" option: if ($ line = ~ /((b.*grass) $/m) {} Now the program will output the following information: Found BrownGrass Similarly, the period can match all the characters except the wrapper: while () {if (/ 19(.*) {IF ($ 1 <20) {$ Year = 2000 $ 1;} else {$ Year = 1900 $ 1;}}} If we read "1981" from the file, $ _ will contain "1981n". The sentence in the regular expression matches "8" and "1" without matching "n". This is needed here because the restriction is not an integral part of the date. For a string containing a lot of rows, we might extract large blocks, which may span line separators.

转载请注明原文地址:https://www.9cbs.com/read-27480.html

New Post(0)