Perhaps something like this (You need to provide the 'distance' sub):
use strict; use warnings; my (@gsm_site, @umts_site); while (<DATA>) { 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 til +l we know better $g->{DIST_TO_NEAREST} = distance($g->{LAT},$g->{LON}, $g->{NEARE +ST}{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.00 +7941667 NI0006, 36.86998613, -1.295393144, " NM5286", 36.83137418, -1.17262637 +2 NI0066, 36.82748524, -1.25734101, " EC4140", 37.4580536, -0.53351668,
Note - you should really use a module like Text::CSV, instead of the split hack, above.

        ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall


In reply to Re: Nested foreach loops by NetWallah
in thread Nested foreach loops by keienn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.