Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl how to check if array is still empty?

Tags:

arrays

perl

This should be simple hopefully. I initialize an empty array, do a grep and place the results (if any) in it, and then check if it's empty. Like so:

my @match = (); @match = grep /$pattern/, @someOtherArray; if (#match is empty#) {     #do something! } 

What's the standard way of doing this?

like image 623
JDS Avatar asked Oct 05 '12 18:10

JDS


People also ask

How do you check if an array of arrays is empty?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

How do I empty an array in Perl?

To empty an array in Perl, simply define the array to be equal to an empty array: # Here's an array containing stuff. my @stuff = ("one", "two", "three"); @stuff = (); # Now it's an empty array!

How do I find the length of an array in Perl?

Note: In Perl arrays, the size of an array is always equal to (maximum_index + 1) i.e. And you can find the maximum index of array by using $#array. So @array and scalar @array is always used to find the size of an array.


1 Answers

You will see all of these idioms used to test whether an array is empty.

if (!@match) if (@match == 0) if (scalar @match == 0) 

In scalar context, an array is evaluated as the number of elements it contains.

like image 166
mob Avatar answered Sep 18 '22 09:09

mob