gpt4 book ai didi

tcl - 使用 `lmap` 过滤字符串列表

转载 作者:行者123 更新时间:2023-12-02 15:25:44 25 4
gpt4 key购买 nike

假设我想从列表中获取所有 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] == 5} {return $word} else continue}
# ==> apple

从 lmap 主体“返回”字符串的优雅方式是什么?

最佳答案

主要的选择是使用set 或使用string cat(假设您是最新的)。为了清楚起见,我将下面的示例分成多行:

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
}
}

关于tcl - 使用 `lmap` 过滤字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com