0
votes

I build a dictionary in tcl 8.4 inside a procedure. How do I use the constructed dictionary in another procedure. I have added a sample code of how I construct a dictionary in tcl 8.4. I know tcl 8.5 has in built in 'dict' option, but I have to use tcl 8.4.

proc a {} {
    set test(key1) "value1"
    set test(key2) "value2"
    lappend keylist "key1"
    lappend keylist "key2"
    foreach key $keylist {
            puts "value of $key is $test($key)"
    }
}

So the procedure mentioned above builds a dictionary. But since tcl 8.4 interpreter interprets every line "$test($key)" as a separate variable how can I make it global so that I can use it in another procedure.

2
You really ought to upgrade to 8.5 or 8.6 if you can; 8.4 is no longer supported even for security or build fixes…Donal Fellows

2 Answers

3
votes

You can use the global command to make a variable or array a global one.

For instance:

proc a {} {
    global test
    set test(key1) "value1"
    set test(key2) "value2"
    lappend keylist "key1"
    lappend keylist "key2"
    foreach key $keylist {
        puts "value of $key is $test($key)"
    }
}

a

# Outputs
# value of key1 is value1
# value of key2 is value2

# Now use them in another proc...
prob b {} {
    global test
    puts $test(key1)
    puts $test(key2)
}

b

# Outputs
# value1
# value2
0
votes

If you are able to return the dictionary, you could pass it in to another proc as a parameter. (Would have preferred to put this in the comment section, but I do not have the required rep)