Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Faster Common Hash key hunt.

by maverick (Curate)
on Jan 24, 2002 at 05:41 UTC ( [id://141102]=note: print w/replies, xml ) Need Help??


in reply to Faster Common Hash key hunt

I don't know if this is the fastest way, but I've used this before and it doesn't seem to be too slow. It will use a bit of memory though, as you end up with a hash that has a union of the keys off all the hashes.
# untested, supports the interface you describe sub commonkeys { my $number_of_hashes = scalar(@_); my %union; my @result; foreach (@_) { foreach (keys %{$_}) { $union{$_}++; } } while (my ($key,$count) = each %union) { if ($count == $number_of_hashes) { # a key which has appeared the same number of +times as we have hashes, is present in every hash. push(@result,$key); } } return @result; }
Odds are if you do a super search or look in Q/A, somebody else has had to do this too.

HTH

/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://141102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found