TCL array values updation based on command line argument

I am trying to substitute variable value inside array so as to update array values based on command line inputs. e.g. I am receiving IP address as command line argument for my TCL script and trying to update commands with recvd IP value.

My array is:

array set myArr { 1 myCmd1("","abc.txt")
                  2 myCmd2("","xyz.txt")
                  3 myCmd3("","klm.txt")

Here, "" will actually be supplied as command line argument.

I tried doing

array set myArr { 1 myCmd1($myIP,"abc.txt")
                  2 myCmd2($myIP,"xyz.txt")
                  3 myCmd3($myIP,"klm.txt")

and other combinations like ${myIP}, {[set $myIP]} but none is working.

Thanks in advance for any help/inputs.


Use the list command

% set myIP
% array set myArr [list 1 myCmd1($myIP,"abc.txt") 2 myCmd2($myIP,"xyz.txt") 3 myCmd3($myIP,"klm.txt")]
% puts $myArr(1)
% puts $myArr(3)

I think your code will be easier to understand and easier to maintain if you don't try to use array set in this instance. You can get away with it if you are careful (see answers related to using list) but there's really no reason to do it that way when a more readable solution exists.

Here's my solution:

set myArr(1) "myCmd1($myIP,\"abc.txt\")"
set myArr(2) "myCmd2($myIP,\"xyz.txt\")"
set myArr(3) "myCmd3($myIP,\"klm.txt\")"


array set myArr [list myCmd1($myIP, "abc.txt") 2 myCmd2($myIP, "xyz.txt") ... ]

Why? Because when you write {$var} in Tcl, it means $var and not the contents of the variable var. If you use list to create the list instead of the braces, variables are still evaluated.

Need Your Help

Drag and drop large virtual files from C# to Windows Explorer

c# com drag-and-drop

I have a C# WPF application with a section that serves as an FTP client, listing files on a remote server and allowing the user to download them. I want the user to be able to drag and drop files f...

What are the type parameter naming guidelines?

c# .net generics naming-conventions

I noticed, as well as saw in the Essential C# 3.0 book, that paramters are usually defined as T or TEntity