How to remove a value from an array in Perl

Source: Internet
Author: User

This article focuses on how to remove a value from an array in Perl? This article explains how to assign the elements of an array to undef, and then delete the elements from the array, and the friends you need can refer to the

I'm not sure if undef has a definite relationship with the elimination of values from the array, guess what, if we treat undef as "empty," there will be some connection. But generally, assigning something to undef and deleting something is different.

First look at how to assign the elements of an array to undef, and then learn how to remove elements from the array.

Start with the following code:

The code is as follows:

Use Data::D umper qw (dumper);

My @dwarfs = QW (Doc grumpy Happy sleepy Sneezy Dopey);

Print dumper @dwarfs;

When you print with data::D Umper, you get the following output:

The code is as follows:

$VAR 1 = [

' Doc ',

' Grumpy ',

' Happy ',

' Sleepy ',

' Sneezy ',

' Dopey ',

' Bashful '

];

Assign an element to undef

Use the return value of the undef () function:

The code is as follows:

Use Data::D umper qw (dumper);

My @dwarfs = QW (Doc grumpy Happy sleepy Sneezy Dopey);

$dwarfs [3] = undef;

Print dumper @dwarfs;

The code assigns the number 3rd element (the 4th element in the array) to undef, but does not change the size of the array:

The code is as follows:

$VAR 1 = [

' Doc ',

' Grumpy ',

' Happy ',

Undef

' Sneezy ',

' Dopey ',

' Bashful '

];

Using the undef () function for an element of a direct array also produces the same result:

The code is as follows:

Use Data::D umper qw (dumper);

My @dwarfs = QW (Doc grumpy Happy sleepy Sneezy Dopey);

Undef $dwarfs [3];

Print dumper @dwarfs;

Therefore, $dwarfs [3] = undef; and undef $dwarfs [3]; the function is the same, they can assign the value to undef.

To remove an element from an array using splice

The splice function removes the element from the array completely:

The code is as follows:

Use Data::D umper qw (dumper);

My @dwarfs = QW (Doc grumpy Happy sleepy Sneezy Dopey);

Splice @dwarfs, 3, 1;

Print dumper @dwarfs;

$VAR 1 = [

' Doc ',

' Grumpy ',

' Happy ',

' Sneezy ',

' Dopey ',

' Bashful '

];

As you can see in this example, the array shortens a unit because we remove an element from the middle of the array.

This is how to remove an element from the array.

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.