Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Common hash keys

by parv (Parson)
on Jun 07, 2008 at 07:30 UTC ( [id://690803]=note: print w/replies, xml ) Need Help??


in reply to Common hash keys

What have you tried? Pseudocode would be ...

for all keys in hash-1, where k-1 is current key for all keys in hash-2, where k-2 is current key put k-1 in store if k-1 = k-2 end-for end-for # "store" would have all the common keys in the end

Time passes. Oh yes, finding if any keys are present ...

common set to false for all keys in hash-1, where k-1 is current key for all keys in hash-2, where k-2 is current key if k-1 = k-2, common set to true break out of both loops end-for end-for # test common to check for commonality

Replies are listed 'Best First'.
Re^2: Common hash keys
by moritz (Cardinal) on Jun 07, 2008 at 07:56 UTC
    Your inner loop could be described as clubbing somebody to death with a loaded uzi. No need to iterate over the hash keys just to check if one is present. Looking up a key in a hash is the most basic operation a hash offers ;-)

      (: Indeed! (But I did not write Perl, did I?)

        Maybe
        my @keys_in_commmon = grep $hashref1->{$_}, keys %$hashref2;
        would've sufficed?? If you just have to determine if they have keys in common,
        if( grep $hashref1->{$_}, keys %$hashref2 ) { ... }
        would do the trick...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found