If I have a proc that needs to return an array to its caller, what is the best way to do this?
I following code does not work in Tcl due to inability to $ an array variable:
proc mine {} {
array set foo { red 1 blue 2 green 3 }
$foo
}
tcl> set foo [mine]
Error: can't read "foo": variable is array
Alternatively this doesn't work either:
proc mine {} {
array set foo { red 1 blue 2 green 3 }
array get foo
}
tcl> set foo [mine]
tcl> puts $foo(blue)
Error: can't read "foo(blue)": variable isn't array
This leaves me with the probably inefficient:
proc mine {} {
array set foo { red 1 blue 2 green 3 }
array get foo
}
tcl> array set foo [mine]
2
Or the obscure:
proc mine {varName} {
upvar $varName localVar
array set localVar { red 1 blue 2 green 3 }
}
tcl>unset foo
tcl>mine foo
tcl>puts $foo(blue)
2
Is there a better way to do this, or if not, which is the most efficient?