List passed to join doesn't produce the correct output

半城伤御伤魂 提交于 2021-02-11 14:57:39

问题


I collect some information using the following method:

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
}

and capture the result like this:

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"]

I need to add it to a list so that I can join it to create a path.

Following the join documentation I tried this:

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

Suppose I answered with Microsoft and Software Engineer as the input, I expected to get:

Microsoft\Software Engineer\20200509-1108

Instead I get:

$company_name\$position\yearmonthday

Can someone elaborate on why? and how to fix it?


回答1:


It's only coming down to quoting. So essentially what you should do instead is to use something that's not braces because you want to allow for variable substitution:

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

The above works, but the recommended way to do it in this case, since you're effectively joining a list, is to use the list command:

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


来源:https://stackoverflow.com/questions/61699000/list-passed-to-join-doesnt-produce-the-correct-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!