Hi Monks, I have a csv file with six columns as below;

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,

Am calling a subroutine 'distance' to calculate the distance between NameA and NameB. 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. I imagine a hash of arrays with NameA as keys will be appropriate to hold that result, but first i need to get over the loop part which i think is a nested for or foreach loop or such. Being new to Perl, I have not used the CSV module, wanted to try split first for better understanding. Below is a part of the relevant code and a sample of the output am getting;

open COMBINED, "<$outf" or die "couldn't open $outf\n"; while (<COMBINED>) { chomp $_; no warnings 'uninitialized'; @coords = (split /,/,$_); my $lat2G = $coords[2]; my $lon2G = $coords[1]; my $lat3G = $coords[5]; my $lon3G = $coords[4]; my $gsm_site = $coords[0]; my $umts_site = $coords[3]; #} my @coords2G = ($lat2G, $lon2G,); my @coords3G = ($lat3G, $lon3G,); foreach my $coord2G(@coords2G) { print $gsm_site,$umts_site,' ', distance(@coords2G, @coord +s3G, "K")."\n"; } }

The comma's are manually inserted for readability, not part of the output (which is tab I think)
NameA, NameB, 0
NameA, NameB, 0
10001_NI0001, WL3762, 254.534319729278
10001_NI0001, WL3762, 254.534319729278
NI0006, NM5286, 14.309312591142
NI0006, NM5286, 14.3093125911424


In reply to 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.