AutomatedDriving
AutomatedDriving

Reputation: 13

How to pass array as argument in TCL from one proc to another proc

I am reading some values from a file and reading them as data(signal1),data(signal2) in one proc - read(). From this proc I am calling another proc usage_12(data(signal1)) and want to send data(signal1) as argument and use in usage_12(args)

proc read() {args} {
    #import data from a file and store {signal1 signal2} data
    usage_12 $data(signal1)
}

proc usage_12 {args} {
   foreach trq $args {
      #iterate for all values
   }
}

want to iterate in usage_12 for all values in data(signal1)

data(signal1) example contents:

1 2 3 4 5 6

Upvotes: 1

Views: 3397

Answers (2)

Drektz
Drektz

Reputation: 95

you can use array get arrayname and array set arrayname for this purpose. It is done in following steps.

  • convert array into list using array get arrayname
  • pass the list as argument
  • convert, if you want, list into array using array set arrayname $list And then you will be able to use copy of array in the proc.
proc print_array {key_vals} {
        array set data $key_vals
        foreach key [array names data] {
            puts $data($key)
        }
    }
array set data [list 0 sdf 1 sddsf 2 dssd]
print_array [array get data]

Note: If you want to use same array ie not copy of array, you can use upvar.

Upvotes: 0

Jerry
Jerry

Reputation: 71538

args is a special variable name for procs, you either need to change it, or indicate that you want to iterate on the elements of the first argument only:

proc usage_12 {data} {
    foreach trq $data {
        puts $trq     ;# Just printing the value
        #iterate for all values
    }
}

Or

proc usage_12 {args} {
    foreach trq [lindex $args 0] {
        puts $trq     ;# Just printing the value
        #iterate for all values
    }
}

Both of the above will print 1 through 6 on their own line.

Upvotes: 2

Related Questions