In Perl, sets can be represented using hash tables. You can check if two sets are equal by comparing their elements.
In this example,
%set1
and %set2
.%set1 = (1 => 1, 2 => 1, 3 => 1);
%set2 = (1 => 1, 2 => 1, 3 => 1);
sub sets_are_equal {
my ($set1_ref, $set2_ref) = @_;
return 0 unless scalar keys %$set1_ref == scalar keys %$set2_ref;
for my $key (keys %$set1_ref) {
return 0 unless exists $set2_ref->{$key};
}
return 1;
}
print sets_are_equal(\%set1, \%set2); # Output: 1
1
In this example,
%set1
and %set2
.%set1 = (1 => 1, 2 => 1, 3 => 1);
%set2 = (4 => 1, 5 => 1, 6 => 1);
sub sets_are_equal {
my ($set1_ref, $set2_ref) = @_;
return 0 unless scalar keys %$set1_ref == scalar keys %$set2_ref;
for my $key (keys %$set1_ref) {
return 0 unless exists $set2_ref->{$key};
}
return 1;
}
print sets_are_equal(\%set1, \%set2); # Output: 0
0
In this tutorial, we learned How to check if Two Sets are Equal in Perl language with well detailed examples.