in reply to Comparing two hashes for duplicate keys with no case sensitivity

Hi, Anonymous -

I understand you want to check case-insensitively which keys in hash2 are also keys of hash1. The following solution might be a good option for large hashes as it only involves one loop:
my $string = join " ", keys %hash1; foreach my $item ( keys( %hash2 ) ) { print "$item matches" if ( $string =~ /\b$item\b/i ); }
  • Comment on Re: Comparing two hashes for duplicate keys with no case sensitivity
  • Download Code

Replies are listed 'Best First'.
Re^2: Comparing two hashes for duplicate keys with no case sensitivity
by chromatic (Archbishop) on Aug 15, 2008 at 01:07 UTC

    That's clever, but I count two loops (and you should probably use \Q...\E as well).

      Hm ... where is the second loop you're seeing, chromatic? Is the join operator an undercover loop as well? Clearly, if the complexity of the join operation is equivalent to that of a regular for loop we don't gain much.

      Cheers -

      Pat

        Yes, join is O(n) where n in this code is the number of keys in the hash. Perl's implementation is slightly more efficient for join than Perl-side iteration, but that's why there's a join operator.