To check if a Set is empty in Perl, you can check if the Set has any elements using the scalar
function.
In this example,
%set
and initialize it with some elements, or an empty Set if there are no elements.scalar
function to get the number of elements in the Set %set
.my %set = (); # Or initialize with elements for a non-empty Set
my $isEmpty = scalar(keys %set) == 0;
if ($isEmpty) {
print "The Set is empty\n";
} else {
print "The Set is not empty\n";
}
The Set is empty
In this example,
%emptySet
.scalar
function to get the number of elements in the Set %emptySet
.my %emptySet = ();
my $isEmpty = scalar(keys %emptySet) == 0;
if ($isEmpty) {
print "The Set is empty\n";
} else {
print "The Set is not empty\n";
}
The Set is empty
In this tutorial, we learned How to check if a Set is Empty in Perl language with well detailed examples.