Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print the Contents of an Array in Tcl

Tags:

tcl

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?

like image 731
Stefan Avatar asked Aug 01 '14 15:08

Stefan


People also ask

Which command display each element of array in Tcl?

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.

How do you pass an array to a function in Tcl?

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.

What is parray in Tcl?

parray prints an array's keys and values, in key alphabetic order and formatted to the longest key name, to stdout.

What is associative array in Tcl?

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.


1 Answers

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"
}
like image 65
Jerry Avatar answered Sep 18 '22 15:09

Jerry