Summary of several methods for Perl to delete array elements

Source: Internet
Author: User

1. Use the grep Function

Function Name grep
Call the syntax @ foundlist = grep (pattern, @ searchlist );
Similar to the Unix search tool with the same name, the grep function extracts elements that match the specified pattern in the list. The pattern parameter is the pattern to be searched, and the return value is the list of matching elements.
Example @ list = ("this", "is", "A", "test ");
@ Foundlist = grep (/^ [TT]/, @ list );
Result @ foundlist = ("this", "test ");

2. Map Functions

Function Name Map
Call the syntax @ resultlist = map (expr, @ list );
This function is defined in perl5. Each element in the list can be used as the operand of the expression expr. It does not change and the result is returned. In the expression expr, the system variable $ _ represents each element.
Example
1. @ list = (100,200,300 );
@ Results = map ($ _ + 1, @ list );

2. @ Results = map (& mysub ($ _), @ list );
Result 1: (101,201,301)
2. None

3. Use splice or delete

Function Name splice
Call the syntax @ retval = splice (@ array, slipelements, length, @ newlist );

Note: The concatenation function can insert an element into the List (array), delete a sublist, or replace a sublist. The skipelements parameter is the number of elements skipped before splicing, length is the number of elements to be replaced, and newlist is the list to be spliced in. When the length of newlist is greater than the length, the subsequent elements are automatically moved backward, and vice versa. Therefore, when length = 0, it is equivalent to inserting an element into the list, which is like a statement.
Splice (@ array,-1, 0, "hello ");

Add an element to the end of the array. When newlist is empty, it is equivalent to deleting the sublist. If length is empty, it is deleted from the skipelements element, and the last element is deleted: splice (@ array,-1); in this case, the returned value is the list of deleted elements.

Both of them can directly Delete the array or hash elements according to the index. However, after the delete operation deletes an element, the element following the index does not move forward. After the element is deleted, an UNDEF element is left in the array, which is obviously not clean enough.

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.