Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble accessing entire contents of a list at one time.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0

I am having trouble using lists in Tcl when I need to input multiple arguements with an executable. For example:

If I would like to run wordpad to edit a number of different files I would type:
exec wordpad.exe jack.txt bob.txt tim.txt

I tried to perform the same thing but now I placed the names of these files in a list "my_list". If I type:
exec wordpad.exe $my_list
Wordpad generates an error saying the it cannot read the file "jack.exe bob.txt tim.txt".

For some reason Tcl treates all the arguements in a list as one long string. How can I make a list return all of it's contents at one time but not as one long string?
 
Although this isn't quite technically correct, you can think of the Tcl interpreter evaluating each command in the follow steps:
[ol][li]Group the arguments based on whitespace characters and quoting[/li][li]Perform all substitutions[/li][li]Execute the result, using the first "word" as the command to execute, passing all of the following words as arguments to the command[/li][/ol]
In your example, the Tcl interpreter regards "$myList" as a single argument, which happens to contain whitespace characters after the substitution is performed. Thus, the value of myList (for example, "jim.txt bob.txt tim.txt") gets passed as a single argument to exec, which passed it as a single argument to wordpad.exe when it executes that program.

To solve this problem, you're going to need to use another Tcl command called eval, which concatenates all of its arguments together (just like the concat command), and then executes the result. In this example, the following should work correctly:

Code:
eval exec wordpad.exe $myList

When eval executes, it see 3 arguments: "exec", "wordpad.exe" and the value of myList, let's say "jim.txt bob.txt tim.txt". Then then concatenates these arguments together into something that in this case would look like:

Code:
exec wordpad.exe jim.txt bob.txt tim.txt

eval then executes this result, which is what you wanted.
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
408-983-1199 Voice
408-983-1198 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top