传递给join的列表没有产生正确的输出。

问题描述 投票:0回答:1

我用下面的方法收集一些信息。

proc getJobinfo {question} {
    puts -nonewline "$question: "
    flush stdout
    gets stdin answer
    set cleanedanswer [string trim [::textutil::string::capEachWord $answer]]
    if {$cleanedanswer eq ""} {
       throw {Value Empty} {Input cannot be empty!}
    }
    return $cleanedanswer
}

采集到的结果是这样的

set systemTime [clock seconds]
set yearmonthday [clock format $systemTime -format %Y%m%d-%H%M%S]

set company_name [getJobinfo "Company Name"]
set position [getJobinfo "Position"]

我需要把它添加到一个列表中 这样我就可以把它加入到一个路径中了

按照 join 文档我试了一下。

set submission_path [join {$company_name $position $yearmonthday} "\\"]

假设我的回答是 MicrosoftSoftware Engineer 作为输入,我以为会得到。

Microsoft\Software Engineer\20200509-1108

相反,我得到的是:

$company_name\$position\yearmonthday

谁能详细解释一下为什么,以及如何解决这个问题?

string tcl
1个回答
2
投票

现在只剩下 引述. 所以基本上你应该做的是使用一些不是大括号的东西,因为你想允许变量替换。

set submission_path [join "$company_name $position $yearmonthday" "\\"]

上面的方法可行,但在这种情况下,推荐的方法是使用list命令,因为你实际上是在加入一个列表。

set submission_path [join [list $company_name $position $yearmonthday] "\\"]
© www.soinside.com 2019 - 2024. All rights reserved.