How to initialize an array in Tcl?

What is the correct way to initialize an empty array in Tcl?

I have the following code (simplified):

proc parseFile {filename results_array} { upvar $results_array results set results(key) $value } set r1 {} parseFile "filename" r1 

and I get the error:

Error: cannot set "results (key)": the variable is not an array

+7
arrays tcl
source share
3 answers

You do not initialize arrays in Tcl, they appear only when the member is set:

 proc stash {key array_name value} { upvar $array_name a set a($key) $value } stash one pvr 1 stash two pvr 2 array names pvr 

gives:

 two one 
+4
source share

To initialize an array, use a "set of arrays". If you just want to create an internal array object without giving it any values, you can give it an empty list as an argument. For example:

 array set foo {} 

If you want to give it values, you can give it a correctly quoted list of key / value pairs:

 array set foo { one {this is element 1} two {this is element 2} } 
+17
source share

installation marks (English) 80

set the value of server_name (key)

0
source share

All Articles