Tcl 使用'lmap'筛选字符串列表
假设我想从列表中获取所有5个字母的单词Tcl 使用'lmap'筛选字符串列表,tcl,Tcl,假设我想从列表中获取所有5个字母的单词 set words {apple banana grape pear peach} lmap word $words {if {[string length $word] == 5} {expr {"$word"}} else continue} # ==> apple grape peach 我不喜欢引用expr{“$word”}的混乱。我希望这能奏效: lmap word $words {if {[string length $word] ==
set words {apple banana grape pear peach}
lmap word $words {if {[string length $word] == 5} {expr {"$word"}} else continue}
# ==> apple grape peach
我不喜欢引用expr{“$word”}
的混乱。我希望这能奏效:
lmap word $words {if {[string length $word] == 5} {return $word} else continue}
# ==> apple
从lmap主体“返回”字符串的优雅方式是什么?我通常使用
set
:
lmap word $words {if {[string length $word] == 5} {set word} else continue}
或者有时(如果我确定expr
不会重新解释word
中的值):
当然,还有这样一个问题:
lsearch -regexp -all -inline $words ^.{5}$
文档:,,,主要选择是使用
set
或使用string cat
(假设您是最新的)。为了清晰起见,我将下面的示例拆分为多行:
lmap word $words {
if {[string length $word] != 5} {
continue
};
set word
}
等等,为什么我认为我需要把引号塞进
expr{$word}
?
lmap word $words {
if {[string length $word] != 5} {
continue
};
set word
}
lmap word $words {
if {[string length $word] == 5} {
# Requires 8.6.3 or later
string cat $word
} else {
continue
}
}