In Perl, you can use the scalar
function to check if an array is not empty.
In this example,
@numbers
with some elements.scalar
function on the array, and store the result in the variable $notEmpty
.@numbers = (1, 2, 3);
$notEmpty = scalar(@numbers);
print "Array is not empty: ", $notEmpty ? 'true' : 'false', "\n";
Array is not empty: true
In this example,
@emptyArray
.scalar
function on the array, and store the result in the variable $notEmpty
.@emptyArray = ();
$notEmpty = scalar(@emptyArray);
print "Array is not empty: ", $notEmpty ? 'true' : 'false', "\n";
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in Perl language with well detailed examples.