Multiple ways to remove elements from an array in Perl

Source: Internet
Author: User

The array element in Perl starts with a reference ordinal of 0, the first element of the @array is $array[0], increments sequentially, and the last element is $array[-1] or $ #array. If you want to delete an existing element in an array, you can do so using several functions.

1. Function name grep
call syntax @foundlist = grep (pattern, @searchlist);
explanation Similar to the UNIX find tool with the same name, the grep function extracts the elements in the list that match the specified pattern, the pattern of which is the mode to find, 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. Function name Map
call syntax @resultlist = map (expr, @list);
explanation This function is defined in PERL5, and it is possible to operate the elements of the list as operands of the expression expr, which itself does not change, and the result is returned as a value. In expression expr, the system variable $_ represents the individual elements.
Example 1, @list = (100, 200, 300);
@results = Map ($_+1, @list);
2, @results = Map (&mysub ($_), @list); #这里没太看明白
Results 1, (101, 201, 301)
2. No

3. Function name Splice
call syntax   @retval = Splice (@array, slipelements, length, @newlist);
explanation   stitching function You can insert an element 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 to be replaced, and NewList is          the list that will be stitched in. When the newlist is longer than length, the back element automatically moves backward, and the inverse       is indented forward. Thus, when length=0, it is equivalent to inserting an element into the list, while the shape, such as statement
     splice (@array,-1, 0, "Hello"), adds an element to the end of the array "Hello". When         newlist is empty, it is equivalent to deleting a sub-list, and if length is empty, it is from     All            skipelements elements are deleted, while the last element is deleted: Splice (@ Array,        -1), in which case the return value is the list of elements that were deleted.

Example @array = qw/a b c d E F g/;

@newlist = QW/AA BB cc dd/;

@retval = Splice (@array, 3, 2, @newlist);  

result @array = qw/ a b c AA bb cc dd f g/;

@retval = qw/d e/;

4. Name of the function delete

Call Syntax delete $array [index];

Explanation Removes the element of the specified index from the array, the value of index is 0, and when index is greater than the array length, the function delete array has no effect; After the delete element is deleted, the element after the index does not move forward. After the element has been deleted, there is a undef element in the array , and obviously the deletion is not clean enough.

examples @fruits = ("Apple", "banana", "berry", "orange");

Print "1: @fruits". " \ n ";

Delete $fruits [1];

Print "2: @fruits". " \ n ";

Print "3: $fruits [1]". " \ n ";

Delete $fruits [5];

Print "4: @fruits". " \ n ";

[Email protected];

Print "5:". $size;

Results 1:apple Banana Berry Orange

2:apple Berry Orange

3:

4:apple Berry Orange

5:4

Multiple ways to remove elements from an array in Perl

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.