I think I lifted this from wikit many winters ago:
# lists and arrays with sugar
set rgx {([0-9A-Za-z_]+):([0-9A-Za-z_]+)$}
if [regexp $rgx [lindex $args 0] -> foo _bar] {
upvar $foo _foo
# if it is array...
if [-a _foo] {return $_foo($_bar)}
# and if it is list...
return [lindex $_foo $_bar]
}
Goal:
% array set test {a 1 b 2 c 3 d 4}
% puts [test:a]
1
% puts [test:b]
2
How is writing [test:a] better than $test(a)?
It's better when 'test' is a variable (probably in a loop) whose value
is the name of an array (looping through a list of arrays) and I can write
[$test:a]
instead of
[set $test(a)]
especially when I want to access that array in the global scope from inside
a proc, which in proper Tcl would be
[set ::${::test}(a)]
Yikes!
But my proc often fails inside procs. Not always, but often, which puzzles me.
Is this practice dangerous? Why? Is there a better way?
I am using the yikes! method for now because it's safer, but I think it's a monstrosity and really wish I could use a cleaner syntax.
Note I only use my overloaded 'unknown' proc and interp aliases in scripts
that I'm sure will never be shared.
--
Luc
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)