Perl File Operations Summary _perl

Source: Internet
Author: User
Tags sprintf

first, open, close the file

The return value of open is used to determine whether the operation that opened the file succeeds, returns a value other than 0 when it succeeds, and returns zero when it fails, so it can be judged as follows:
if (Open (MYFILE, "MYFILE"))
{
# Here are what to do if the file opened successfully
}
When the file operation is finished, use Close (MYFILE); Closes the file.
READ: Open (file handle, "< filename") open (file handle, "filename") the prerequisite file must already exist, otherwise it will return 0, error message in!
Write: Open (file handle, "> filename"), if the file does not exist, then create it, if it exists, the content is emptied, the length is truncated to 0,! error message.
Append: Open (file handle, ">> filename"), basically same write, but one thing, the contents of the file will not be emptied, the new content will be appended to the original text.
Read and write: Open (file handle, "+< filename"), through "+<" mode, you can both read the file, and you can write the file. You can move within a file through the tell () function and navigate 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 purged.

Second, read the document

Statement line = <MYFILE>; reads a row of data from a file and stores it in a simple variable line and moves the file pointer back one row.
The statement @array = <MYFILE>; reads the entire contents of the file into an array @array, and each line of the file (including a carriage return) is an element of @array.

The main use of the following three functions:

1__ve_item__øtell function

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

Length = tell (FILE);

A record has 3 byte lengths, byte 0,1 and 2. Thus, the current position in the file is 3, which is the length of a record.

1__ve_item__øseek function

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

function seek accepts 3 parameters: filename, offset, and method.

Offset is the number of bytes that need to be moved when the new current position is determined in the file. This number can be calculated in this way: the number of records that need to be skipped multiplied by the number of bytes recorded.

Method indicates where you want to start the lookup:

0 means to calculate from the beginning of the file;

1 indicates that the calculation starts at the current position;

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

For example:

Seek (FILE, 5*length, 0)

After execution, he uses the computed 5*length and method to skip the first 5 records of the file for 0来, and the 6th record as the current location of the file

1__ve_item__øtruncate function

The TRUNCATE function receives two parameters: a file handle and a file byte position. It starts removing records from the file byte until the file ends.

Perl String processing functions

Perl String handler function index

Call Syntax Position=index (string,substring,position);
The narration returns the position of the substring substring in string strings, or returns 1 if it does not exist. Parameter position is optional, indicating the number of characters skipped before the match, or starting from that position.

Perl String processing functions Rindex

Call Syntax Position=rindex (string,substring,position);
The explanation is similar to index, except that it matches from the right side.

Perl String processing function length

Call Syntax Num=length (string);
EXPLANATION returns the length of the string, or the number of characters it contains.

Perl String processing function pos

Call Syntax Offset=pos (string);
EXPLANATION returns the position of the last pattern match.

Perl String processing functions substr

Call Syntax substr (expr,skipchars,length)
Explanation Extract string (or string generated by expression) the substring in expr, skipping skipchars characters, or starting from position Skipchars (the first character position is 0), length of substring, and this parameter can be ignored, meaning that all remaining characters are taken.
When this function appears to the left of the equation, expr must be a variable or an array element, where some of the substrings are replaced by the value on the right side of the equation.

Perl String Processing functions Study

Call syntax study (scalar);
The explanation uses an internal format to improve the access speed of a variable, and at the same time it works on only one variable.

Perl String processing function LC
Uc
Call Syntax Retval=lc (string);
Retval=uc (string);
Explanation converts the string into small/uppercase letters.

Perl String processing functions Lcfirst

Ucfirst
Call Syntax Retval=lcfirst (string);
Retval=ucfirst (string);
The explanation converts the first letter to small/uppercase.

Perl String processing functions Quotameta

Call Syntax Newstring=quotemeta (oldstring);
The narration adds a backslash (\) to the front of the letter that is not a word.
Statement: String=quotemeta (String);
Equivalent to: string=~s/(\\w)/\\\\1/g;
Often used in pattern matching operations, ensure that no characters in the string are treated as matching operators.

Perl string processing function join

Call syntax join (joinstr,list);
The narration combines a string list (array) into a long string, inserting a string of joinstr between each of the two list elements.

Perl String processing functions sprintf

Call Syntax sprintf (string,fields);
The explanation is similar to printf, except that the result is not output to a file, but is assigned to a variable as a return value.
Example num=26;
outstr=sprintf ("%d=%xhexadecimalor%ooctal\", num,num,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.