Perl file operation (2) (suitable for beginners)

xiaoxiao2021-03-06  74

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 =; 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.

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

New Post(0)