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.