Reputation: 137
Need help in finding all particular values after certain match 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
This is an exact example of the scenario, I would like to take output of destinations so when I found node 24784 in the second row which I can find with array(node) then I would like to display all remaining destinations and then when my required node is 24800 then I just need only two destinations as an output i.e: 12.g3.55.b7.76.19
and 10.b1.59.v5.25.33
Upvotes: 0
Views: 54
Reputation: 13272
The basic structure here is list of associative arrays, but Tcl arrays don't really lend themselves to being put in lists. It would be easier to put Tcl dicts in a list.
You can get a list of dicts like this:
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]
Then you can find the list index of the dict that holds the searched-for node like this:
set node 24784
set idx [lsearch -index 5 $data $node]
and print out the list of destinations like this:
if {$idx >= 0} {
puts [lmap item [lrange $data $idx end] {dict get $item destination}]
}
The number of remaining destinations is [llength [lrange $data $idx end]]
, assuming that $idx >= 0
.
Documentation: dict, if, llength, lmap, lmap replacement, lrange, lsearch, puts, set, split, string
Upvotes: 1