0

I got the following error from the TCL interpreter when trying to get the value of env variable:

puts $env
can't read "env": variable is array

How can get the value of a TCL variable if it is an array?

1

1 Answer 1

2

parray was introduced in Tcl8.5 and is used to 'puts' arrays:

parray env

Note that you need to provide the array name and not the array variable.

Otherwise, you can use a loop:

foreach {key value} [array get env] {
    puts "env($key) = $value"
}
Sign up to request clarification or add additional context in comments.

4 Comments

I thought it was around long before that. I remember using it in Tcl 7.4...
@DonalFellows I just inferred that from the link in the wiki oops. I'm not really sure for how long it's been around, but if it's from 7.4, then I'm wrong there.
Plus there are lots more array subcommands. For example: foreach key [array names env] {puts "env($key) = $env($key)"}
According to the Tcl changes file (the only way to do code archaeology that far back quickly) it predates 6.5, as the changes for 6.5 document a behaviour change for it (probably a switch to using upvar instead of global) implying that it must've been there before and had users. It's all before my time going that far back...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.