有一個列表中搜索獲取所有索引且搜索項在列表中,不僅是第一個
set haystack [list a b c d e e f e]
我想找到的所有索引,其中搜索字符串出現在草垛:
set needle e
我曾嘗試使用
set foundat [expr {[lsearch -all $haystack $needle] >= 0}]
但我沒有得到預期的答案4 5 7.
有一個列表中搜索獲取所有索引且搜索項在列表中,不僅是第一個
set haystack [list a b c d e e f e]
我想找到的所有索引,其中搜索字符串出現在草垛:
set needle e
我曾嘗試使用
set foundat [expr {[lsearch -all $haystack $needle] >= 0}]
但我沒有得到預期的答案4 5 7.
命令:
set foundat [expr {[lsearch -all $haystack $needle] >= 0}]
發現所有的地方是$needle
在$haystack
併產生所有這些地方的列表。這是一個數字列表。然後它將該列表作爲字符串與值0
進行比較,除非該列表中只有一個項目,但效果相同,因爲它基本上最終會爲找到值的任何結果生成真值。但你真的只想要:
set foundat [lsearch -all $haystack $needle]
set haveFoundAny [expr { [llength $foundat] != 0 }]
沒有找到?空的清單。發現了什麼?非空列表。你剛纔提到
set haystack [list a b c d e e f e]
set needle e
這會給你想要的東西
這會給你想要的東西:
lsearch -all $haystack $needle
你寫什麼:在foundat
set foundat [expr {[lsearch -all $haystack $needle] >= 0}]
地方如果搜索發現還是不行。
列表:
set e_index [lsearch -all $haystack $needle]
puts $e_index
'設置foundat [lsearch -all $大海撈針$針]'給你想要的東西。 –