in reply to Nested foreach loops

Here's where am stuck, how do I loop the first item on NameA column to iterate over all NameBs and return the smallest distance, together with both corresponding NameA and NameB entries then move to the next row and repeat above, and so on till end of file.

What does that mean?

?"Loop first item"? What distance?

If you could please use the numbers from the three lines of data you posted, and in pencil-and-paper fashion , solve the problem step by step for those three lines, this would help me understand what you're asking

Its hard to interpret a program that doesn't work to understand the question being asked

Probably need to read the file into a hash of arrays first, then query it like a database, do some calculations ...