In Perl, you can access items in a set using iteration or specific methods provided by modules like Set::Scalar.
In this example,
string_set
containing strings.use Set::Scalar;
my $string_set = Set::Scalar->new(qw(Apple Banana Orange Grapes));
foreach my $item ($string_set->members()) {
print "$item\n";
}
Apple Banana Orange Grapes
In this example,
number_set
containing integers.size
method to get the number of elements in the set.includes
method to check if an item exists in the set.use Set::Scalar;
my $number_set = Set::Scalar->new(1, 2, 3, 4);
print "Number of elements: " . $number_set->size() . "\n";
print "Does set contain 3? " . ($number_set->includes(3) ? 'true' : 'false') . "\n";
Number of elements: 4 Does set contain 3? true
In this tutorial, we learned How to Access Items in a Set in Perl language with well detailed examples.