use strict; use warnings; my (@gsm_site, @umts_site); while () { my ($name1,$lon1,$lat1, $name2,$lon2,$lat2) = split /[\s"]*,["\s]*/, $_; next unless $lat1 and $lon1; # Avoid title lines push @gsm_site, {NAME=>$name1, LAT=>$lat1, LON=>$lon1}; push @umts_site,{NAME=>$name2, LAT=>$lat2, LON=>$lon2}; } for my $g (@gsm_site){ $g->{NEAREST} = $umts_site[0]; # Initial assumption till we know better $g->{DIST_TO_NEAREST} = distance($g->{LAT},$g->{LON}, $g->{NEAREST}{LAT},$g->{NEAREST}{LON}, "K"); for my $u (@umts_site[1..$#umts_site]){ my $this_distance = distance($g->{LAT},$g->{LON}, $u->{LAT},$u->{LON}, "K"); next unless $this_distance < $g->{DIST_TO_NEAREST}; $g->{NEAREST} = $u; $g->{DIST_TO_NEAREST} = $this_distance; } } # Print them out for my $g (@gsm_site){ print "$g->{NAME}, $g->{NEAREST}->{NAME}, $g->{DIST_TO_NEAREST},\n"; } __DATA__ NameA Longitude Latitude NameB Longitude Latitude 10001_NI0001, 36.79887354, -1.26122956, " WL3762", 34.52328889 , -1.007941667 NI0006, 36.86998613, -1.295393144, " NM5286", 36.83137418, -1.172626372 NI0066, 36.82748524, -1.25734101, " EC4140", 37.4580536, -0.53351668,