How to Print the Contents of an Array in Tcl
Asked Answered
M

1

16

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?

Mehetabel answered 1/8, 2014 at 15:38 Comment(0)
P
24

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"
}
Possessory answered 1/8, 2014 at 16:17 Comment(1)
parray is just a procedure; you can use info body to see how it works (it uses array names, lsort, foreach and format…)Bourgeoisie

© 2022 - 2024 — McMap. All rights reserved.