Perl file operation summary

Source: Internet
Author: User

1. Open and Close files

The return value of open is used to determine whether the file is successfully opened. If the file is successfully opened, a non-zero value is returned, and zero is returned if the file fails. Therefore, the following judgment can be made:
If (open (MYFILE, "myfile "))
{
# Here is what to do if the file opened successfully
}
After the file operation is completed, close the file with MYFILE.
Read: open (file handle, "<file name") open (file handle, "file name"). The prerequisite file must already exist. Otherwise, 0 is returned. The error message is displayed! Medium
Write: open (file handle, "> file name"). If the file does not exist, create it. If yes, the content is cleared and the length is 0 ,! There is error information.
Append: open (file handle, "> file name"), which is basically the same as write. However, the content in the file will not be cleared, and the new content will be appended to the end of the original text.
Read/write: open (file handle, "+ <file name"). In the "+ <" mode, you can read and write files. You can use the tell () function to move inside the file and locate it through the seek () function. If the file does not exist, it will be created. If the file already exists, the original data will not be cleared.

Ii. Reading files

Statement line = <MYFILE>; read a row of data from the file and store it in the simple variable line, and move the file pointer back to a row.
Statement @ array = <MYFILE>; read all the content of the file into the array @ array. each row of the file (including the carriage return) is an element of @ array.

The following functions are used:

1incluve_item _ Ø tell Function

The tell function is used to determine the current position of the file (that is, the byte after the first record ). The value returned by Tell is also the number of bytes of the record length.

Length = tell (FILE );

For example, a record contains three bytes in length, 1 or 2. Therefore, the current position in the file is 3, which is the length of a record.

1incluve_item _ Ø seek Function

If the record length is known, the function seek can be used to find any records in the file.

The seek function accepts three parameters: filename, offset, and method.

Offset is the number of bytes to be moved when a new position is determined in the file. This number can be calculated as follows: the number of records to be skipped is multiplied by the number of records in bytes.

Method indicates the location to start searching:

0 indicates computing from the beginning of the file;

1 indicates that the calculation starts from the current position;

2 indicates that the calculation starts from the end of the file (in this case, the offset is generally negative ).

For example:

Seek (FILE, 5 * length, 0)

After execution, it uses formula 5 * length and method 0 to skip the first five records of the file and uses 6th records as the current position of the file.

1incluve_item _ Ø truncate Function

The truncate function receives two parameters: a file handle and a file byte location. It removes records from the file byte until the end of the file.

Perl string processing functions

Perl string processing function index

Call the syntax position = index (string, substring, position );
Returns the position of the substring in the string. If not, returns-1. The position parameter is optional, indicating the number of characters skipped before matching, or starting from this position.

Perl string processing function rindex

Call the syntax position = rindex (string, substring, position );
The description is similar to the index, with the difference being matching from the right side.

Perl string processing function length

Call the syntax num = length (string );
Returns the length of a string, or the number of characters.

Perl string processing function pos

Call syntax offset = pos (string );
Returns the position of the last pattern match.

Perl string processing function substr

Call the syntax substr (expr, skipchars, length)
Describe the substring In the extracted string (or the string generated by the expression) expr, skip the skipchars character, or extract the substring from the position skipchars (the first character is 0 ), the length of the substring is length. This parameter can be ignored, meaning that all the remaining characters are taken.
When this function appears on the left of the equation, expr must be a variable or an array element. In this case, the molecular string in the middle of the function is replaced by the value on the right of the equation.

Perl string processing function study

Call the syntax study (scalar );
An internal format is used to increase the access speed of a variable. At the same time, it only takes effect for one variable.

Perl string processing function lc
Uc
Call the syntax retval = lc (string );
Retval = uc (string );
Converts all strings to uppercase or lowercase letters.

Perl string processing function lcfirst

Ucfirst
Call the syntax retval = lcfirst (string );
Retval = ucfirst (string );
The description converts the first letter into uppercase or lowercase letters.

Perl string processing function quotameta

Call the syntax newstring = quotemeta (oldstring );
Add a backslash (\) to the front of a non-word letter (\\).
Statement: string = quotemeta (string );
Equivalent to: string = ~ S/(\ W)/\ 1/g;
It is often used in pattern matching to ensure that no character in the string is considered as a matching operator.

Perl string processing function join

Call the syntax join (joinstr, list );
Comments: Combine the string list (array) into a long string and insert the string joinstr between each two list elements.

Perl string processing function sprintf

Call the syntax sprintf (string, fields );
Similar to printf, the difference is that the result is not output to the file, but is assigned to the variable as the return value.
Example num = 26;
Outstr = sprintf ("% d = % xhexadecimalor % ooctal \", num );
Print (outstr );
Result output 26 = 1ahexadecimalor32octal

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.