I agree with Fletch's suggestion that a good structure for merging your data is a "hash of hashes", so let me just point out some gotchas in your input data which you might want to clean up:
- The columns are named ID1 and ID2 in one input file but ID_1 and ID_2 in the other.
- The second input file has an extra column Distance so just counting the elements from which the identifier is built won't work.
- These may be a PerlMonks copypaste artifact: Both input files start with an empty line, and the separator isn't a tab. Perhaps it is more robust to split /\s+/ instead?