in reply to One loop or two

If your script works well enough as it is, performance-wise, a solution might be quite simple ...

  1. Outside of the loop, declare:
    my $matchingbkid;.
  2. Where you now have print FILE2 ... NONBKID, replace with:
    $matchingbkid = "NONBKID"; # DEFAULT ASSUMPTION .
  3. In the inner loop, when you find the match, write:
    if ($ntid eq $fields[1]) { $matchingbkid = $bkid; last; # END THE LOOP - WE FOUND IT }
  4. Now, after the end of the inner loop:
    print FILE2 "$fields[0]:$fields1:$result:$matchingbkid\n";

The double-loop strategy in this example is retained ... if it works fast enough for your purposes there is no need to redesign it.   The difference is that the inner loop starts with the assumption that the key will be NONBKID, then sets about to try to prove otherwise.   When the first matching key is discovered, it captures the key then quits looking further.   A single output record is generated for either case.

The # COMMENTS are merely for clarity.

Replies are listed 'Best First'.
Re^2: One loop or two
by Anonymous Monk on Aug 01, 2012 at 14:49 UTC
    Excellent. A simple solution staring me in the face. Thank you very much