On Mon, 2 Jul 2001, 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.

You only need one loop to search; if a key doesn't exist in one hash, then
you already know it doesn't exist in both hashes, so why search the other
one?

  while (($key, $value) = each (%todayhash)) {
    if (exists($yesterdayhash{$key})) {
      $delta = $value - $yesterdayhash{$key};
      # Do something with $delta, presumably
    }
  }

> 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.

That is because you are searching hashes.  Hashes don't need to be
searched; they're hashes!  Look at my above code and see if you can see
where your logical flaw was.

- D

<[EMAIL PROTECTED]>

Reply via email to