Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an array in Tcl?

Tags:

arrays

tcl

What is the proper 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: can't set "results(key)": variable isn't array

like image 685
Amir Rachum Avatar asked Jul 28 '10 04:07

Amir Rachum


1 Answers

To initialize an array, use "array set". If you want to just create the 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 properly quoted list of key/value pairs:

array set foo {
    one {this is element 1}
    two {this is element 2}
}
like image 74
Bryan Oakley Avatar answered Sep 28 '22 17:09

Bryan Oakley