Passing a dictionary value to and fro in TCL
I am trying to write a script which uses a dictionary value to store some data. I am then using the same dictionary value to a procedure for further processing. It looks something like this :
proc testProc {a} {
some statements ...
dict incr a key2 1
}
set dummy [dict create]
dict incr dummy key1 1
testProc $dummy
puts "[dict get $dummy]"
I expected to get output as :
key1 1 key2 1
Instead I got :
key1 1
If I want my output first one , then how should i do it.
Thanks
Solution 1:
testProc
updates a copy of the dict. You can either return the modified one and save it:
proc testProc {a} {
# some statements ...
dict incr a key2 1
return $a
}
set dummy [dict create]
dict incr dummy key1 1
set dummy [testProc $dummy]
puts [dict get $dummy]
or pass the name of the variable holding the dict and use upvar
(Similar to how dict incr
etc. works):
proc testProc {aDict} {
upvar $aDict a
# some statements ...
dict incr a key2 1
}
set dummy [dict create]
dict incr dummy key1 1
testProc dummy
puts [dict get $dummy]