I want to print the contents of an array in Tcl (for debugging). The order is unimportant, I just want every value printed.
How do I do it?
array startsearch arrayName This command initializes an element-by-element search through the array given by arrayName, such that invocations of the array nextelement command will return the names of the individual elements in the array.
If you want to pass an array, which s essentially a collection of variables, to a procedure in TCL, you can use the command `upvar` as explained earlier. You are passing a name to the procedure and then using it to access the array at the previous level.
parray prints an array's keys and values, in key alphabetic order and formatted to the longest key name, to stdout.
Associative Arrays In Tcl, all arrays by nature are associative. Arrays are stored and retrieved without any specific order. Associative arrays have an index that is not necessarily a number, and can be sparsely populated. A simple example for associative array with non-number indices is shown below.
The simplest way would be to use parray
:
% array set val [list a 1 b 2 c 3]
% parray val
val(a) = 1
val(b) = 2
val(c) = 3
If you want just the key and the value, well, use a loop and array get
:
foreach {key value} [array get val] {
puts "$key => $value"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With