【发布时间】:2019-09-26 02:56:56
【问题描述】:
我有一个 excel 表(.csv 文件),其中包含不同交通摄像头位置的 lats 和 longs,并且我构建了一个使用 haversine 和 geocoder gem 的 Ruby 程序来查找用户输入的地址与 CSV 文件中不同摄像头位置之间的距离,然后打印出离用户提供的地址最近的摄像头。
我的问题是,它打印出最近的摄像头的整行信息,我希望它只打印出行中的第一个值(最近的交通摄像头位置的交点)。
CSV 文件与此类似:
位置 纬度 经度 运营
圣。查尔斯@华盛顿大街 29.93102 -90.086 是的
所以我的问题是,为了简化,我如何让它打印出来:
St. Charles @ Washington Ave. is 1.0351466822038633 miles away!
代替:
St. Charles @ Washington Ave.,30.015934,-90.075197,Y is 1.0351466822038633 miles away!
我不希望它打印出额外的值,例如 lat、long 和运行状态 (Y),只是摄像头交叉点的名称。
这是代码的 sn-p(不要给出我的太多代码,以防其他学生将来收到相同的作业并需要自己弄清楚):
require 'csv'
require 'haversine'
require 'geocoder'
# ***missing code that does all of the work converting the distance between the given address
# and the addresses in the .CSV file and finds the closest traffic camera***
min_num = distance #in miles, calculated by Haversine and Geocoder
# reads the line of the CSV file with the shortest distance to the input address and assigns value to the variable 'closest_cam'
closest_cam = CSV.readlines("./cameras.csv", headers: true)[index_num]
# prints the closest camera to the given address and how far away it is (in miles),
# the problem is that it prints the entire row, with lats, longs, etc. instead of just the name of the closest camera intersection.. ):<
puts "#{closest_cam} is #{min_num} miles away!"
【问题讨论】:
标签: ruby csv readline readlines