Several ways to remove an array element from Perl summary _perl
Source: Internet
Author: User
1. Using the grep function
Function name grep
Call Syntax @foundlist = grep (pattern, @searchlist);
Explanation similar to the UNIX lookup tool with the same name, the grep function extracts the element in the list that matches the specified pattern, the parameter pattern is the mode to look for, and the return value is a list of matching elements.
Example @list = ("This", "is", "a", "test");
@foundlist = grep (/^[tt]/, @list);
The result @foundlist = ("This", "test");
2. Using the Map function
Function name Map
Call Syntax @resultlist = map (expr, @list);
To explain this function as defined in PERL5, you can use each element in the list as an operand of the expression expr, which does not change itself, as the return value. In expression expr, the system variable $_ represents each element.
Example
1, @list = (100, 200, 300);
@results = Map ($_+1, @list);
Splice of Function name
Call Syntax @retval = Splice (@array, slipelements, length, @newlist);
Narrator: a concatenation function can insert elements into the middle of a list (array), delete a child list, or replace a child list. Parameter skipelements is the number of elements skipped before stitching, length is the number of elements replaced, and NewList is the list that will be spliced in. When the length of the newlist is greater, the following elements are automatically moved backward and the reverse is indented forward. Therefore, when length=0, it is equivalent to inserting an element into the list, and the shape of a statement
Splice (@array,-1, 0, "Hello");
Adds an element to the end of the array. When the newlist is empty, it is equivalent to deleting the child list, and if length is empty, it is removed from the first skipelements element and the last element is deleted: Splice (@array,-1); In this case, the return value is the list of deleted elements.
Both can delete array or hash elements directly according to the index. However, after deleting the element, the element behind the index does not move forward actively, after the element is deleted, there is still a undef element in the array that is clearly not clean enough.
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.