空のリスト要素を除外したいだけの場合は、次のようにするのが明らかです。
# Assuming the original list is in $list
set result {}
foreach x $list {
if {[string trim $x] != ""} {
lappend result $x
}
}
# The result list should contain the cleaned up list.
[string trim]
すべての空の要素が本当に空であり、空白が含まれていないことが確実な場合 ({}
おそらく の代わりに意味する) 、 を実行する必要がないことに注意してください{ }
。ただし、例には空の要素と空白の両方が含まれているため、文字列のトリムを行う必要があります。
または、正規表現を使用してテストすることもできます。
foreach x $list {
# Test if $x contains non-whitespace characters:
if {[regexp {\S} $x]} {
lappend result $x
}
}
ただし、lsearch を使用して上記を 1 行で実行できます。
# Find all elements that contain non whitespace characters:
set result [lsearch -inline -all -regexp $list {\S}]