To check if a specific item is present in a Set in Perl, you can use the exists
function.
In this example,
%my_set
containing elements of a specific type.exists
function to check if a specific item is present in the Set.exists
function returns 1
if the item is present in the Set, otherwise it returns undef
.# Define Set with items
my %my_set = ("apple" => 1, "banana" => 1, "orange" => 1, "grape" => 1, "melon" => 1);
# Check if item is present
my $item = "banana";
my $is_present = exists $my_set{$item};
# Print result
print "Is $item present in the Set? ", $is_present ? "true" : "false", "\n";
Is banana present in the Set? true
In this example,
%my_set
containing elements of a specific type.exists
function to check if a specific item is not present in the Set.exists
function returns undef
if the item is not present in the Set.# Define Set with items
my %my_set = ("apple" => 1, "banana" => 1, "orange" => 1, "grape" => 1, "melon" => 1);
# Check if item is not present
my $item = "peach";
my $is_not_present = !exists $my_set{$item};
# Print result
print "Is $item not present in the Set? ", $is_not_present ? "true" : "false", "\n";
Is peach not present in the Set? true
In this tutorial, we learned How to check if a Specific Item is present in the Set in Perl language with well detailed examples.