|
From: Peter S <pet...@gm...> - 2017-02-14 16:31:23
|
On 14/02/2017, avl <av...@lo...> wrote:
>
> Apart from current state of tip-282 (with quoting var- and array-
> names, or using results of sub-expressions as names), it's the only
> other way to also support array-element-assignments.
>
Is quoting that bad?
expr {
"arr(sin)" = sin($x);
"arr(cos)" = cos($x);
"arr(tan)" = tan($x)
}
expr {
{arr(sin)} = sin($x);
{arr(cos)} = cos($x);
{arr(tan)} = tan($x)
}
Probably still better than:
set arr(sin) [expr {sin($x)}]
set arr(cos) [expr {cos($x)}]
set arr(tan) [expr {tan($x)}]
Also possible:
expr {
s = sin($x);
c = cos($x);
t = tan($x)
}
lassign [list $s $c $t] arr(sin) arr(cos) arr(tan)
lassign [expr {
list (sin($x), cos($x), tan($x))
}] arr(sin) arr(cos) arr(tan)
Technically, it's possible to create helper functions that return a
name of an array variable, but that's not any better than quoting the
array name...
proc array {name index} {
return "$name\($index)"
}
interp alias {} tcl::mathfunc::array {} ::array
expr {
[array arr sin] = sin($x);
[array arr cos] = cos($x);
[array arr tan] = tan($x)
}
I'd rather choose simply quoting the variable.
During experimenting with this, I get a weird error that I don't understand:
% proc array {name index} {return "$name\($index)"}
% set [array foo bar] 1
1
% parray foo
can't use non-numeric string as operand of "!"
% set ::errorInfo
can't use non-numeric string as operand of "!"
while executing
"if {![array size UnknownPending]} {
unset UnknownPending
}"
(procedure "::unknown" line 37)
invoked from within
"parray foo"
% set [array foo baz] 2
2
% parray foo
can't use non-numeric string as operand of "!"
% set ::errorInfo
can't use non-numeric string as operand of "!"
while executing
"if {![array exists array]} {
return -code error "\"$a\" isn't an array"
}"
(procedure "parray" line 3)
invoked from within
"parray foo"
% puts $foo(bar)
1
% puts $foo(baz)
2
I totally don't understand this error.
|