At 01:08 PM 7/2/01 -0500, Perl Beginner wrote:
>Hello,
>
>I'm your typical beginner who is kind of stuck. Here is the problem:
>
>I have two hashes, each has ~40,000 key/value pairs. I need to find which
>keys in one hash are the same as the keys in the other hash. The ones that
>have the same key then need their values diffed. This is how I was trying to
>accomplish this:
>
>  while (($key1,$value1) = each (%yesterdayhash)) {
>   while (($key2,$value2) = each (%todayhash))  {
>
>    if (($key1 eq $key2) {
>       $delta = ($value2 - $value1) ;
>    }
>   }
>  }
>
>I let this run for over 48 hours and it never completed! One of our Perl
>experts said I did a big no-no. Everything in those while statements will be
>iterating ~40,000 * 40,000 times.
>
>My question is how else can I find the same keys in two hashes and then diff
>their values without taking forever?

How cruel of your Perl expert to tell you you were wrong without spending 5 
seconds to give you the answer.  The problem you are trying to solve is why 
hashes were invented: constant lookup time.  Hence:

while (($key1, $value1) = each %yesterdayhash) {
   next unless exists $todayhash{$key1};
   $delta = $todayhash{$key1} - $value1;
}


--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com

Reply via email to