如何在TCL中的每一行放置\和\n

如何在TCL中的每一行放置\和\n,tcl,newline,slash,Tcl,Newline,Slash,我正在尝试搜索每个匹配的文件,并在文件末尾放入\n和\n 这是我的剧本: foreach aaa $list { set alist [glob "hier/hier1/hier2/*.lib"] puts $filename [join $alist "\ \\\n"] } 我得到的结果如下: file1 \ file2 \ file3 \ file4 问题是文件4的结尾似乎没有\号?我不能轻易地在puts语句中添加另一个\项,因为它

我正在尝试搜索每个匹配的文件,并在文件末尾放入\n和\n

这是我的剧本:

foreach aaa $list {
    set alist [glob "hier/hier1/hier2/*.lib"]
    puts $filename [join $alist "\ \\\n"]
}
我得到的结果如下:

file1 \
file2 \
file3 \
file4
问题是文件4的结尾似乎没有\号?我不能轻易地在puts语句中添加另一个\项,因为它将提供双\。那么,我的puts语句是否有错误,没有将\放在最后一行

file1 \\
file2 \\
file3 \\
file4

如果你有一个列表
{abc}
并且你
加入$list:
你会得到
a:b:c
,在最后一个元素后面没有冒号。这就是这里发生的事情

你想要这样的东西:

# add a space and backslash to each element of alist
set with_continuations [lmap element $alist {string cat $element " " \\}]
# then print joined with newlines
puts $filename [join $with_continuations \n]

使用foreach而不是lmap:

set with_continuations {}
foreach element $alist {
    lappend with_continuations [string cat $element " " \\]
}

如果你有一个列表
{abc}
并且你
加入$list:
你会得到
a:b:c
,在最后一个元素后面没有冒号。这就是这里发生的事情

你想要这样的东西:

# add a space and backslash to each element of alist
set with_continuations [lmap element $alist {string cat $element " " \\}]
# then print joined with newlines
puts $filename [join $with_continuations \n]

使用foreach而不是lmap:

set with_continuations {}
foreach element $alist {
    lappend with_continuations [string cat $element " " \\]
}

一个优雅的、最小的解决方案——很好。也可以在答案中添加一个显式的迭代解吗?我认为它可能会帮助其他人了解
lmap
如何在这种情况下封闭循环。一个优雅的、最小的解决方案——很好。也可以在答案中添加一个显式的迭代解吗?我认为这可能有助于其他人了解
lmap
如何在这种情况下封闭循环。