Perl-08-array library functions

Source: Internet
Author: User

1. array concatenation function: splice

Used to insert an element or sublist to a list or array. You can also delete an element or sublist from an array or list, or replace an element or sublist in an array or list; the prototype is as follows:

@ Retvalue = splice (@ array, skipelements, length, @ newlist );

The skipelements parameter is the number of elements skipped before splicing, that is, the number of elements to be replaced starts from the skipellements element. Length indicates the number of elements to be replaced; @ newlist is an array or list to be spliced recently. When the length of @ newlist is greater than the length, the following elements are automatically moved backward, and vice versa. Therefore, when length = 0, it is equivalent to inserting an element into an array or list. For example:

Splice (@ array,-, "hello"); # adds an element to the end of the array;

When @ newlist is empty, it is equivalent to deleting an element or sublist from an array or list. If length is empty, delete all elements after the skipelements element, for example:

Splice (@ array, 5); # deletes all elements after the first element;

Splice (@ array,-1); # deletes the last element in the array or list. The returned value is the list of deleted elements;

Splice (@ array, 5, 1); # Delete the element after the first element in the array @ array;

2. array sorting function: Sort ()/reverse ()

The sort () function sorts arrays or lists and returns the sorted array or list;

The reverse () function sorts all elements in the array or list in alphabetical order and returns the processed array or list;

3. Queue functions:

In Perl, arrays or lists can be used as queues;

Queue header operation functions: shift () and unshitf ()

$ Element = shift (@ array): deletes the first element in the array, moves all the remaining elements forward, and returns the deleted element. If no parameter is added, the array @ argv is operated by default;

$ COUNT = unshift (@ array, elements): In contrast to the function shift (), this function adds one or more elements before the first element of the array, the returned value is the length of the result list or array;

Team-end operation functions: Push () and POP ()

$ Element = POP (@ array): deletes the last element of the array and returns the deleted element. If the list is empty, an undefined value (null) is returned );

$ COUNT = Push (@ array, elements): adds one or more elements to the end of an array or list and returns the length of the result list or array;

4. VEC functions:VEC ()

VEC is a vector function. It treats the value of the simple variable $ vector as multiple pieces of (dimension) data. Each piece of data contains a certain number of BITs. Together, it is a vector data. Each call accesses a piece of data, which can be read or written. The $ index parameter is like the subscript of the array, indicating which part to access. 0 is the first part, and so on, note that the access order is from right to left, that is, the first block is on the far right. The bits parameter specifies the number of digits in each block, values can be 1, 2, 4, 8, 16, or 32. The call method is as follows:

$ Retval = VEC ($ vector, $ index, $ bits );

5. Other array Functions

@ Resultlist = map (expr, @ list );

This function calculates the elements in @ list as the operands of the expression expr. The elements in @ list remain unchanged, store the processing results of each element in @ list to a new array or list, and use this new array or list as the return value. In the expression expr, the system variable "$ _" indicates each element in @ list;

$ Formatstring = pack (packformat, @ list): converts a list or array to a format (packaged) stored on the machine or used by programming languages such as C) to a simple variable. The packformat parameter contains one or more format characters. Each element in the list @ list corresponds to one. Each format character can be separated by space or tab key. Pack () the function ignores spaces;

For the format characters stored in the packformat parameter, if you need to repeat a certain format multiple times, you can add an integer after this format character to indicate the number of times used;

For example, $ twoints = pack ("I2", 103,241); # "I2" indicates that the format is I2;

If you want to apply the same format to an array or all elements in the list, you can add an asterisk (*) after the characters in this format. For example:

$ Manyints = pack ("I *", 14,26, 11,83 );

Other relevant format characters can be viewed at any time when writing Perl programs;

The most common purpose of a pack () function is to create data that can interact with a C program. For example, a string in C ends with null, 0, or '/0, you can create such string data in Perl:

$ Cstring = pack ("ax", $ mystring );

@ List = unpack (packformat, $ formatstring): this function is opposite to the pack () function. It is used to convert values stored in machine format into a list of Perl values; the format characters are basically the same as those of the pack () function;

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.