I don't think your code is doing what you want. Examine the following:
LIAM:~$ perl push(@a, 'a'); push(@a, 'b'); $foo{stuff} = @a; print $foo{stuff} . "\n";
gives the result of 2. This is because you're assigning into the hash value the result of (scalar @a). If you really want to go with a hash of lists, your code should look more like
$myhash{$key1}=\@value1
Instead, you probably should be using a hash of hashes, e.g.
$myhash{$key1} = {'abd' => 1, 'bcd' => 2, ...};
This will prevent duplicate elements from messing up your set. Then, you can use sort and keys together to do the compare. Here's a subroutine that does what I think you're looking for:
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 }
Note that this code is very untested. Here's some minimal test code:
my %foo; $foo{set1} = {a=>'beta', 'c'=>'gamma'}; add_set(\%foo, "set2", 'beta','gamma'); add_set(\%foo, "set3", 'moo', 'cow'); print join("\n", keys %foo);
Thinking about it, Quantum::Superpositions might be a better way to do this. There also might be a fairly obvious better way to do this that I'm not seeing. Anyhow, I hope this helps.

In reply to Re: How to compare hash value by Improv
in thread How to compare hash value by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.