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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

OK, let's walk through this.

A hash reference is just a reference to a hash, getting at the hash is a simple matter -- so we can ignore the "reference" part of your question for now, and focus on how to determine if two hashes share any keys. Let's start with two simple hashes:

%a = ( one=>1, two=>2, three=> 3 ); %b = ( two=>'two', three=>'three', four=>'four');

We know the keys they have in common are two and three, but how to find that out in code?

First, let's rephrase the question a little bit: if I know all the keys in one hash, how do I find out if any of those keys exist in another hash?

Let's work backwards from that. How do we find out if the %a hash contains a key -- let's say, the key one? That would be exists:

if (exists $a{one}) { print "\%a has 'one' for a key" }

Ok, so how do we find out what keys a hash has? How about keys:

print join(",", keys %a); # prints "one,two,three"

So, if we get the list of keys for hash %a, all we have to do is loop through them and see if %b has any that match. Here's the long way:

my @common_keys; foreach my $key (keys %a) { if (exists $b{$key}) { print "\%a and \%b have key '$key' in common\n"; push @common_keys, $key; } }

Make sense? Of course, this type of activity -- looking through a list to find items that meet a criterion -- is so common that Perl has a function for it: grep. So here's a short version:

my @common_keys = grep { exists $b{$_} } keys(%a);

Now let's add "references" back to the mix. Let's make references to our hashes:

$x = \%a; $y = \%b;

Now, we do as above, but dereferencing as appropriate:

my @common_keys = grep { exists $y->{$_} } keys( %{ $x } );

We pass the whole hash referenced by $x to keys; and we use the dereference operator -> to make sure that exists looks at the hash referenced by $y.

<radiant.matrix>
Ramblings and references
“A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” — Herm Albright
I haven't found a problem yet that can't be solved by a well-placed trebuchet

In reply to Re: Common hash keys by radiantmatrix
in thread Common hash keys 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-23 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found