在 Tcl 中使用 LIST 的问题

Issue in using a LIST in Tcl

我已经按照以下格式定义了一个变量。

set ast_protocol_FilePath {
  abcdefghijk.spf
}

我的代码以下列方式调用这个变量。

if { [info exists ast_protocol_FilePath] && ( [llength $ast_protocol_FilePath]) }  { 
    run_drc $ast_protocol_FilePath
}

但是,由于列表的定义方式,我看到了一个错误。 $ast_protocol_FilePath 是

Error: Unable to open read file "
    abcdefghijk.spf
"

理想情况下,$ast_protocol_FilePath 应该定义为“abcdefghijk.spf”,而不是新行格式。

这里有出路吗?我不想将列表定义为

set ast_protocol_FilePath { abcdefghijk.spf }

您可以使用 string trim 删除前导和尾随空格:

run_drc [string trim $ast_protocol_FilePath]

或者如果您想继续将变量视为列表,lindex:

run_drc [lindex $ast_protocol_FilePath 0]

除了,您还可以这样做:

run_drc {*}$ast_protocol_FilePath

That(扩展语法)接受列表中的单词并将它们分别用作单独的参数。当只有一个单词和一些额外的空格和换行符时,就像您的情况一样,它会摆脱那个讨厌的空格。