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?
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.
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With