查找数组中的下一个所有值
Finding next all values in Array
需要帮助查找数组中某些匹配项后的所有特定值
Alpha 24835 line 24837 node 24780 destination 11.a1.v2.bt.13.91 next 24801
Alpha 24840 line 22543 node 24784 destination 10.a1.32.b2.12.10 next 24637
Alpha 24855 line 24734 node 24798 destination 10.a1.cb.62.41.31 next 24564
Alpha 24861 line 24947 node 24800 destination 12.g3.55.b7.76.19 next 24435
Alpha 24890 line 23538 node 24880 destination 10.b1.59.v5.25.33 next 24543
这是场景的一个确切示例,我想输出目的地,所以当我在第二行找到节点 24784 时,我可以用 array(node) 找到它,然后我想显示所有剩余的目的地然后当我需要的节点是 24800 时,我只需要两个目的地作为输出,即:12.g3.55.b7.76.19
和 10.b1.59.v5.25.33
这里的基本结构是关联数组列表,但是Tcl 数组并不真正适合放在列表中。将 Tcl 指令放入列表中会更容易。
您可以获得这样的字典列表:
set data [split [string trim {
Alpha 24835 line 24837 node 24780 destination 11.a1.v2.bt.13.91 next 24801
Alpha 24840 line 22543 node 24784 destination 10.a1.32.b2.12.10 next 24637
Alpha 24855 line 24734 node 24798 destination 10.a1.cb.62.41.31 next 24564
Alpha 24861 line 24947 node 24800 destination 12.g3.55.b7.76.19 next 24435
Alpha 24890 line 23538 node 24880 destination 10.b1.59.v5.25.33 next 24543
}] \n]
然后你可以找到包含搜索节点的dict的列表索引,如下所示:
set node 24784
set idx [lsearch -index 5 $data $node]
并像这样打印出目的地列表:
if {$idx >= 0} {
puts [lmap item [lrange $data $idx end] {dict get $item destination}]
}
剩余目的地数量为[llength [lrange $data $idx end]]
,假设$idx >= 0
。
文档:dict, if, llength, lmap, lmap replacement, lrange, lsearch, puts, set, split, string
需要帮助查找数组中某些匹配项后的所有特定值
Alpha 24835 line 24837 node 24780 destination 11.a1.v2.bt.13.91 next 24801
Alpha 24840 line 22543 node 24784 destination 10.a1.32.b2.12.10 next 24637
Alpha 24855 line 24734 node 24798 destination 10.a1.cb.62.41.31 next 24564
Alpha 24861 line 24947 node 24800 destination 12.g3.55.b7.76.19 next 24435
Alpha 24890 line 23538 node 24880 destination 10.b1.59.v5.25.33 next 24543
这是场景的一个确切示例,我想输出目的地,所以当我在第二行找到节点 24784 时,我可以用 array(node) 找到它,然后我想显示所有剩余的目的地然后当我需要的节点是 24800 时,我只需要两个目的地作为输出,即:12.g3.55.b7.76.19
和 10.b1.59.v5.25.33
这里的基本结构是关联数组列表,但是Tcl 数组并不真正适合放在列表中。将 Tcl 指令放入列表中会更容易。
您可以获得这样的字典列表:
set data [split [string trim {
Alpha 24835 line 24837 node 24780 destination 11.a1.v2.bt.13.91 next 24801
Alpha 24840 line 22543 node 24784 destination 10.a1.32.b2.12.10 next 24637
Alpha 24855 line 24734 node 24798 destination 10.a1.cb.62.41.31 next 24564
Alpha 24861 line 24947 node 24800 destination 12.g3.55.b7.76.19 next 24435
Alpha 24890 line 23538 node 24880 destination 10.b1.59.v5.25.33 next 24543
}] \n]
然后你可以找到包含搜索节点的dict的列表索引,如下所示:
set node 24784
set idx [lsearch -index 5 $data $node]
并像这样打印出目的地列表:
if {$idx >= 0} {
puts [lmap item [lrange $data $idx end] {dict get $item destination}]
}
剩余目的地数量为[llength [lrange $data $idx end]]
,假设$idx >= 0
。
文档:dict, if, llength, lmap, lmap replacement, lrange, lsearch, puts, set, split, string