To get the size of a set in Perl, you can use the scalar
function on the array representing the set.
In this example,
@set1
with some elements.scalar
function on the array @set1
to get the number of elements in the set.scalar
function returns the size of the set.# Create a set
my @set1 = ('apple', 'banana', 'cherry');
# Get the size of the set
my $size = scalar @set1;
# Print the size
print "Size of set1: $size\n";
Size of set1: 3
In this example,
@emptySet
.scalar
function on the array @emptySet
to get the number of elements in the set.scalar
function returns 0.# Create an empty set
my @emptySet = ();
# Get the size of the empty set
my $size = scalar @emptySet;
# Print the size
print "Size of emptySet: $size\n";
Size of emptySet: 0
In this tutorial, we learned How to Get Length of a Set in Perl language with well detailed examples.