How do you remove a value from an array in Perl? _perl

Source: Internet
Author: User

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:

Copy Code code 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:
Copy Code code as follows:

$VAR 1 = [
' Doc ',
' Grumpy ',
' Happy ',
' Sleepy ',
' Sneezy ',
' Dopey ',
' Bashful '
];

assign an element to undef

Use the return value of the undef () function:

Copy Code code 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:
Copy Code code 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:
Copy Code code 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:

Copy Code code 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.