Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 22:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found