Perl file operations (2) (suitable for beginners)

Source: Internet
Author: User
Tags scalar

Read multiple records
If you call this operation, the remaining records in the file are returned. If you are at the end of the file, an empty table is returned:

@ Records =;

If (@ records ){

Print "there were", scalar (@ records), "records read. N ";

}

In the following step, assign values and test two tasks:

If (@ records = ){

Print "there were", scalar (@ records), "records read. N ";

}

Chomp () can also be used for Array Operations:

@ Records =;

Chomp (@ records );

You can perform the chomp operation on any expression, so you can write it in the following step:

Chomp (@ records = );

What is record?

The default record is "row ".

The record definition is controlled by the $/variable. This variable stores the delimiter of the input record, because the line break (according to definition !) Is used to separate rows, so its default value is the string "N ".

For example, you can replace "N" with any symbol you want to replace ".

$/= ";";

$ Record =; # Read the next record separated by semicolons

$/You can take the other two interesting values: Null String ("") and UNDEF.

Read paragraph
$/= "" Is used to indicate that Perl reads a paragraph. A paragraph is a text block consisting of two or more line breaks. This is different from setting as "Nn", which reads only text blocks composed of two lines. In this case, there will be a problem: If a continuous empty row exists, such as "textnnnn", you can either interpret it as a paragraph ("text "), it can also be interpreted as two paragraphs ("text", followed by two linefeeds, and an empty section, followed by two blank lines .)

When reading the text, the second explanation is not very useful. If the paragraph you are reading has the above situation, you do not have to filter out the "empty" section.

$/= "Nn ";

While (){

Chomp;

Next unless length; # skip an empty segment

#...

}

You can set $/to UNDEF, which is used to read the section following two or more linefeeds: UNDEF $ /;

While (){

Chomp;

#...

}

Read the entire file

$/'S other interesting values are UNDEF. If this value is set, Perl is told that the READ command returns the remaining part of the file as a string:

UNDEF $ /;

$ File =;

Because the value of $/is changed, it will affect each subsequent read operation, not only the next read operation. Generally, you need to restrict the operation to a local location. The following example reads the content of the file handle into a string:

{

Local $/= UNDEF;

$ File =;

}

Remember: Perl variables can read long strings. Although your file size cannot exceed your virtual memory capacity, you can still read as much data as possible.
Operations on files using regular expressions
Once you have a variable that contains the entire string, you can use a regular expression to operate the entire file rather than a block in the file. There are two useful regular expressions to mark/s and/m. Generally, the regular expression in Perl is used to process rows. You can write it like this:

UNDEF $ /;

$ Line =;

If ($ line = ~ /(B. * grass) $ /){

Print "found $ 1N ";

}

If we fill in the following content for our file:
Browngrass

Bluegrass

The output is:

Found bluegrass

It does not find "browngrass", because $ only searches for matching at the end of the string (or a row before the end of the string ). If you use "^" and "$" to match strings that contain many rows, you can use the/M ("multiline") option:

If ($ line = ~ /(B. * grass) $/m ){}

Now the program will output the following information:

Found browngrass

Similarly, a period can match all characters except line breaks:

While (){

If (/19 (. *) $ /){

If ($1 <20 ){

$ Year = 2000 + $1;

} Else {

$ Year = 1900 + $1;

}

}

}

If we read "1981" from the file, $ _ will contain "1981n ". The period in the regular expression matches "8" and "1", but does not match "N ". This is what we need to do here, because linefeeds are not part of the date.

For a string containing many rows, we may need to extract large blocks that may span line separators. In this case, we can use the/s option and use a period to match all characters except line breaks.

If (m {(.*?)} S ){

Print "found bold text: $ 1N ";

}

Here, I used {} to indicate the start and end of the regular expression without a slash. So, I can tell Perl that I am matching and the start character is "M ", the end character is "S ". You can combine the/s and/m options:

If (m {^ (.*?)} SM ){

#...

}

Summary
There are two ways to open a file: the open () function is fast and simple, while the sysopen () function is powerful and complex. You can use the operator to read a record. $/variable allows you to control the record. If you want to read the contents of many rows into a string, do not mark the rows with the/s and/M regular expressions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.