如何使用功能"table:get" (表扩展名)何时需要2个键? [英] How to use the function "table:get" (table extension) when 2 keys are required?

查看:101
本文介绍了如何使用功能"table:get" (表扩展名)何时需要2个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.txt文件,其中包含3列:ID-polygon-1,ID-polygon-2和distance. 将文件导入Netlogo时,获得3个列表[[list1][list2][list3]],它们与3列相对应.

I have a file .txt with 3 columns: ID-polygon-1, ID-polygon-2 and distance. When I import my file into Netlogo, I obtain 3 lists [[list1][list2][list3]] which corresponds with the 3 columns.

我用table:from-list list创建了一个包含3个列表内容的表. 我得到{{table: [[1 1] [67 518] [815 127]]}}(该表显示了我的数据集的前两行).

I used table:from-list list to create a table with the content of 3 lists. I obtain {{table: [[1 1] [67 518] [815 127]]}} (The table displays the first two lines of my dataset).

例如,我想获取ID-polygon-1 = 1(list1)和ID-polygon-2 = 67(list1)之间的距离(list3),即815.

For example, I would like to get the value of distance (list3) between ID-polygon-1 = 1 (list1) and ID-polygon-2 = 67 (list1), that is, 815.

当我需要2个键(ID-polygon-1和ID-polygon-2)时如何使用table:get table key?

How can I use table:get table key when I have need of 2 keys (ID-polygon-1 and ID-polygon-2) ?

非常感谢您的帮助.

推荐答案

使用

Using table:from-list will not help you there: it expects "a list of two element lists, or pairs" where the "the first element in the pair is the key and the second element is the value." That's not what you have in your original list.

此外,NetLogo表(和关联数组通常)不能具有两个键.它们始终只是键值对.但是,没有什么能阻止该值成为另一个表,而就您而言,这就是您所需要的:一个表!

Furthermore, NetLogo tables (and associative arrays in general) cannot have two keys. They are always just key-value pairs. Nothing prevents the value from being another table, however, and in your case, that is what you need: a table of tables!

但是,没有直接构建该基元的原语.您将需要自己构建它:

There is no primitive to build that directly, however. You will need to build it yourself:

extensions [ table ]
globals [ t ]

to setup
  let lists [
    [   1   1 ] ; ID-polygon-1 column
    [  67 518 ] ; ID-polygon-2 column
    [ 815 127 ] ; distance column
  ]
  set t table:make
  foreach n-values length first lists [ ? ] [
    let id1  item ? (item 0 lists)
    let id2  item ? (item 1 lists)
    let dist item ? (item 2 lists)
    if not table:has-key? t id1 [
      table:put t id1 table:make
    ]
    table:put (table:get t id1) id2 dist
  ]
end

这是打印结果表时得到的:

Here is what you get when you print the resulting table:

{{table: [[1 {{table: [[67 815] [518 127]]}}]]}}

这是一个小记者,可以很方便地与桌子保持距离:

And here is a small reporter to make it convenient to get a distance from the table:

to-report get-dist [ id1 id2 ]
  report table:get (table:get t id1) id2
end

使用get-dist 1 67将提供您正在寻找的815结果.

Using get-dist 1 67 will give the 815 result you were looking for.

这篇关于如何使用功能"table:get" (表扩展名)何时需要2个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆