To check if a Set contains all the items of another Set in Perl, you can use hash tables.
In this example,
@set1
and @set2
.%hash
where the keys are the elements of @set1
.@set2
and check if it exists in %hash
.@set2
is not found in %hash
, then @set1
does not contain all items of @set2
.# Define Sets with items
my @set1 = (1, 2, 3, 4, 5);
my @set2 = (2, 3);
# Create a hash table
my %hash = map { $_ => 1 } @set1;
# Check if set1 contains all items of set2
my $contains_all = 1;
foreach my $item (@set2) {
unless ($hash{$item}) {
$contains_all = 0;
last;
}
}
# Print result
print "Does set1 contain all items of set2? " . ($contains_all ? "true" : "false") . "\n";
Does set1 contain all items of set2? true
In this example,
@set1
and @set2
.%hash
where the keys are the elements of @set1
.@set2
and check if it exists in %hash
.@set2
is not found in %hash
, then @set1
does not contain all items of @set2
.# Define Sets with items
my @set1 = (1, 2, 3, 4, 5);
my @set2 = (2, 6);
# Create a hash table
my %hash = map { $_ => 1 } @set1;
# Check if set1 contains all items of set2
my $contains_all = 1;
foreach my $item (@set2) {
unless ($hash{$item}) {
$contains_all = 0;
last;
}
}
# Print result
print "Does set1 contain all items of set2? " . ($contains_all ? "true" : "false") . "\n";
Does set1 contain all items of set2? false
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Perl language with well detailed examples.