Home > Back-end >  Issue in using a LIST in Tcl
Issue in using a LIST in Tcl

Time:03-08

I have defined a variable in the following format.

set ast_protocol_FilePath {
  abcdefghijk.spf
}

And my code calls this variable in the following fashion.

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

However, I see an error because of the way list is defined. $ast_protocol_FilePath is

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

ideally the $ast_protocol_FilePath should have been defined as " abcdefghijk.spf " and not in the new line format.

Is there a way out here? I don't want to define list as

set ast_protocol_FilePath { abcdefghijk.spf }

CodePudding user response:

You can use string trim to remove leading and trailing whitespace:

run_drc [string trim $ast_protocol_FilePath]

or if you want to continue to treat the variable as a list, lindex:

run_drc [lindex $ast_protocol_FilePath 0]

CodePudding user response:

Apart from Shawn's suggestions, you can also do this:

run_drc {*}$ast_protocol_FilePath

That (expansion syntax) takes the words in the list and uses them each as a separate argument. When there's just one word and some extra spaces and newlines, as in your case, it gets rid of that pesky whitespace.

  • Related