0

How do you compare one hash's key with other hash's value? Here is the output of my hashes. I only need to return boolean output.

( My code is kind of mess right now, so please forgive me for not posting here. I just need the logic, I will try to work further to incorporate it.)

    $VAR1 = {
      '5555' => [
                    '13570'
                  ]
    };
    $VAR1 = {
      '13570' => [
                   '[04/Jun/2013:15:06:13'
                 ]
            };

Thanks.

John Rein
  • 17
  • 5
  • 1
    [And what have you tried?](http://whathaveyoutried.com) –  Jun 11 '13 at 20:56
  • `defined $VAR1->{$VAR1->{'5555'}}` should tell you if a key matching the value of $VAR1's key '5555' exists in $VAR1 .... for varying definitions of VAR1..... 8/ – tjd Jun 11 '13 at 21:01
  • I tried , | if (defined $hash1{$key2}) | , here key2 refers from the second hash. – John Rein Jun 11 '13 at 21:15

3 Answers3

1

$VAR1 is assigned twice. I guess this is a typo. Do you actually want to check whether a hash contains a given key, although this key is the same (eq) with a value in another hash?

You can use exists, e.g., if (exists $hash{$key}) {...}. For the difference between defined and exists, see What's the difference between exists and defined?

BTW, as you declare anonymous hash with { } and anonymous array with [ ], the de-reference is needed before getting the actual content; otherwise you just use the references (like a pointer in c language).

Community
  • 1
  • 1
yuni
  • 37
  • 1
  • 1
    No, it's not a typo, it's a result of using Data::Dumper to display the hashes. Data::Dumper doesn't have access to the actual names of the variables, so it calls them all `$VAR1`. – Dave Sherohman Jun 12 '13 at 07:47
0

What's throwing you off here is that your hash values aren't the actual values, but rather array references (as shown by the [ ... ] in your Data::Dumper output) pointing to single-element arrays which contain the actual data. So you need to dereference them and grab the first element from the resulting array:

#!/usr/bin/env perl    

use strict;
use warnings;
use 5.010;

my %hash1 = ('5555' => [ '13570' ]);
my %hash2 = ('13570' => [ '[04/Jun/2013:15:06:13' ]);

for (keys %hash1) {
  my $first_key = $_;
  my $second_key = $hash1{$first_key}[0];
  say "$first_key -> $second_key -> $hash2{$second_key}[0]";
}

Output:

5555 -> 13570 -> [04/Jun/2013:15:06:13

Edit: Alternate code to check all values in each %hash1 entry and display all corresponding values for each in %hash2:

#!/usr/bin/env perl    

use strict;
use warnings;
use 5.010;

my %hash1 = ('5555' => [ '13570', '8675309' ]);
my %hash2 = (
  '13570' => [ '[04/Jun/2013:15:06:13' ],
  8675309 => [ 'Jenny', 'I got your number' ],
);


for (keys %hash1) {
  my $first_key = $_;
  for my $second_key ( @{$hash1{$first_key}} ) {
    if (exists $hash2{$second_key}) {
      say "$first_key -> $second_key ->";
      say "\t$_" for @{$hash2{$second_key}};
    }
  } 
}

Output:

5555 -> 13570 ->
    [04/Jun/2013:15:06:13
5555 -> 8675309 ->
    Jenny
    I got your number
Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • Thanks Dave for the response, this is what I was looking for, however as you understand that first hash might contain multiple values, hence need to grep for existence. – John Rein Jun 12 '13 at 08:33
  • @JohnRein See the alternate version I edited in for handling multiple values in the arrays. – Dave Sherohman Jun 12 '13 at 08:56
  • Wow, this is great.Please ignore the second key in hash 2, as it is not common between the two hash. We just need firstkey to compare the value of hash1. – John Rein Jun 12 '13 at 09:37
  • 1
    Why don't you just write it `for my $first_key (...){...}`? – Brad Gilbert Aug 24 '13 at 01:46
-3

I'm getting this as how to compare a non encrypted value to an encrypted value. I could be wrong though.

If this is the case...

if HASH_VALUE(nonEncrypted) is equal to encryptedValue
    do something
else
    do something else
Mike M
  • 752
  • 1
  • 5
  • 13