How do you initialize an array in expect-send script? We can initialize a variable using set Variable_name value. Likewise how can an array be initiated in expect-send script?
2 Answers
I assume you mean array as in associative array, not array as in C-like numerically-indexed list. You use the array set command:
array set varname {key1 val1 key2 val2 key3 val3 ... ...}
Since the contents of braces are not evaluated by Tcl (hence by expect), you can make it pretty if you want
array set varname {
key1 val1
key2 val2
.... ....
}
You can also assign the array elements directly:
set varname(key1) val1
set varname(key2) val2
...
Comments
Note that unlike set used to set a variable, array set works differently:
In the same tclsh:
% array set a {key1 val1 key2 val2}
% parray a
a(key1) = val1
a(key2) = val2
#This will not delete the content of the array:
% array set a {}
% parray a
a(key1) = val1
a(key2) = val2
#This will add new key-value pair:
% array set a {key3 val3}
% parray a
a(key1) = val1
a(key2) = val2
a(key3) = val3
#Array keys are unique:
% array set a {key1 val1 key2 val2}
% parray a
a(key1) = val1
a(key2) = val2
a(key3) = val3
#To unset the entire array content:
array unset a *
#To unset the array:
array unset a