Perl (Scripting) - Remove all elements from array (undef)

by
Jeremy Canfield |
Updated: May 14 2021
| Perl (Scripting) articles
Let's say the @fruit array contains 4 elements.
my @fruit = qw(orange apple peach pear);
In this example, the $count variable will contain the number of elements in the @fruit array.
my $count = "";
$count = @fruit;
You can print the $count variable;
print $count;
Which will return 4, meaning there are 4 elements in the @fruit array.
4
The undef operator can be used to remove all of the values from the array. Once undefined, the array will no longer contain any values.
undef @fruit;
The $count variable can be used again to contain the number of elements in the @fruit array.
$count = @fruit;
You can print the $count variable;
print $count;
Which should return 0, meaning there are now 0 elements in the @fruit array.
0
Did you find this article helpful?
If so, consider buying me a coffee over at