sub add_set { # adds a set if it's unique, otherwise does nothing. my ($mhash, $new_hashid, @elements) = @_; my %endhash; foreach my $element (@elements) {$endhash{$element} = 1;} my @matched_keys = grep { my $truth = 1; foreach my $cand_element (keys %endhash) {$truth *= defined $mhash{$_}{$cand_element}; } } (keys %$mhash); # This only ensures that we have a subset my $magic_key; foreach my $matched_key (@matched_keys) { my $match_ok = 1; foreach my $c_elem (keys %{$mhash{$matched_key}} ) { if(! defined($endhash{$c_elem}) ) {$match_ok = 0;} } if($match_ok) {$magic_key = $matched_key;last;} } if(defined($magic_key) ) { # Our set is already in here, so just return print "Set already found, returning\n"; return; } $$mhash{$new_hashid} = \%endhash; # Otherwise, add it }