Search code examples
arraystclparameter-passingcommand-line-argumentscmdline-args

How can I pass an array as argument to TCL script command line run?


I have script 2 script main.tcl and sub.tcl, Where in main.tcl I create 2 arrays, which I need to pass to the sub.tcl script as command line arguments.

Main.tcl:

   set arr1(0) 100
   set arr1(1) 200

   set arr2(0) 300
   set arr2(1) 400
   set res [exec tclsh.exe sub.tcl arr1 arr2]
   puts $res 

sub.tcl

      set arr1 [lindex $argv 0]
      set arr2 [lindex $argv 1]
      puts [array get arr1]
      puts [array get arr2]

The sub.tcl script consider the array arguments as variables and it does not print any values at the output. Could you please help, how can specify the array while passing array as command line argument.


Solution

  • To pass an array we should use array get <arrayname>. So, you should be calling the sub.tcl, as

    set res [exec tclsh.exe sub.tcl [array get arr1] [array get arr2]]
    

    In sub.tcl, directly print the args as

    array set arr1 [lindex $argv 0]
    array set arr2 [lindex $argv 1]
    parray arr1
    parray arr2