List 获取列表中搜索项所在的所有索引,而不仅仅是第一个索引

List 获取列表中搜索项所在的所有索引,而不仅仅是第一个索引,list,search,tcl,List,Search,Tcl,有要搜索的列表 set haystack [list a b c d e e f e] 我想找到haystack中存在searchstring的所有索引: set needle e 我试着用 set foundat [expr {[lsearch -all $haystack $needle] >= 0}] 但是我没有得到预期的答案4 5 7。这会给你你想要的: lsearch -all $haystack $needle set e_index [lsearch -all $h

有要搜索的列表

set haystack [list a b c d e e f e]
我想找到haystack中存在searchstring的所有索引:

set needle e
我试着用

set foundat [expr {[lsearch -all $haystack  $needle] >= 0}]

但是我没有得到预期的答案4 5 7。

这会给你你想要的:

lsearch -all $haystack $needle
set e_index [lsearch -all $haystack $needle]

puts $e_index
你写的:

set foundat [expr {[lsearch -all $haystack  $needle] >= 0}]

在foundat中放置搜索是否找到的位置。

这将为您提供所需内容:

lsearch -all $haystack $needle
set e_index [lsearch -all $haystack $needle]

puts $e_index
你写的:

set foundat [expr {[lsearch -all $haystack  $needle] >= 0}]
如果找到或未找到搜索,则放置在foundat中。

命令:

set foundat [expr {[lsearch -all $haystack  $needle] >= 0}]
查找
$heystack
$needle
所在的所有位置,并生成所有这些位置的列表。这是一个数字列表。然后,它将该列表作为字符串与值
0
进行比较,除非列表中只有一项,但效果相同,因为它基本上会为找到值的任何结果生成一个真值。但实际上你只想:

set foundat [lsearch -all $haystack $needle]

set haveFoundAny [expr { [llength $foundat] != 0 }]
什么也没找到?空列表。找到什么了吗?非空列表。

命令:

set foundat [expr {[lsearch -all $haystack  $needle] >= 0}]
查找
$heystack
$needle
所在的所有位置,并生成所有这些位置的列表。这是一个数字列表。然后,它将该列表作为字符串与值
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
set e_index [lsearch -all $haystack $needle]

puts $e_index

你提到的清单

set haystack [list a b c d e e f e]

set needle e

这将为您提供您想要的:

lsearch -all $haystack $needle
set e_index [lsearch -all $haystack $needle]

puts $e_index

set foundat[lsearch-all$haystack$needle]
提供您想要的。
set foundat[lsearch-all$haystack$needle]
提供您想要的。