在Array中查找下一个所有值

时间:2016-03-10 08:31:57

标签: scripting tcl

在数组

中的某个匹配项后找到所有特定值时需要帮助
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,我可以找到数组(节点)然后我想显示所有剩余的目的地然后当我的所需节点是24800时,我只需要两个目的地作为输出即:12.g3.55.b7.76.1910.b1.59.v5.25.33

1 个答案:

答案 0 :(得分:1)

这里的基本结构是关联数组列表,但是Tcl数组并不适合放入列表中。将Tcl dicts放在列表中会更容易。

您可以获得这样的词典列表:

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

文档:dictifllengthlmaplmap替代,lrangelsearch,{{ 3}},putssetsplit

相关问题