单个变量的多维数组



我有一个像这样的多维数组,它是一组纬度和经度。

multi = [[12.96145, 77.58408],[12.96219, 77.58447],[12.96302, 77.58489],[12.96316, 77.58496],[12.96348, 77.58511],[12.96356, 77.58512],[12.96363, 77.5851],[12.96372, 77.58506],[12.96376, 77.58497],[12.96374, 77.58479]]

我想把上面的数组转换成单独的变量,如下所示:

lat1 = 12.96145
lon1 = 77.58408
lat2 = 12.96219
lon2 = 77.58447
.
.

我试图通过下面这样一个漫长的过程来实现上述目标。我希望肯定会有更好的方法来做到这一点。

l1,l2,l3,l4,l5,l6,l7,l8,l9,l10 = multi
lat1 = l1.first
lon1 = l1.last
lat2 = l2.first
lon2 = l2.last
.
.
lat1, lon1,
lat2, lon2,
...
latn, lonn,
= multi.flatten

(lat1, lon1),
(lat2, lon2),
...
(latn, lonn),
= multi

也可以为实例变量赋值,如下所示:

  multi.each_with_index do |pair, i|
    instance_variable_set("@lat#{i%multi.size+1}", pair.first)
    instance_variable_set("@lon#{i%multi.size+1}", pair.last)
  end

然后参考变量so:@lat1@lon1。。。,@latn@lonn

相关内容

  • 没有找到相关文章