|
From: EricT <tw...@gm...> - 2025-10-21 00:07:43
|
I believe the reason you got the error was that you actually typed both
statements, setting the scalar null variable and then tried the array null
variable.
I should have made that clear that those two statements weren't to be
executed together. I was illustrating that there were actually 2 null
variables, one for a scalar and one for an array. They are, however,
mutually exclusive.
Once you created the scalar null variable, you have to unset it to use the
array null variable. That's another reason I would stay away from using
null variables, had you done this,
% set foo something
something
% set foo(index) something-else
can't set "foo(index)": variable isn't array
I think then you would have realized the problem instantly.
Also, are you running with the $(expr) enabled code or 8.6 or 9.x? I am
unable to reproduce your exact error. Here's what I get with 8.6
% array set {} { (2+2) 5 }
% puts $((2+2))
can't read "((2+2)": no such element in array
% set ((2+2))
5
However, I believe the problem is that you are running into some parser
issues here that the manual doesn't mention. It does, however, say
Note that variables may contain character sequences other than those listed
above, but in that case other mechanisms must be used to access them (e.g.,
via the *set *command's single-argument form).
And so that is why the above using set does return the value 5. But when
trying $ substitution, the rules are more restrictive, and you likely hit
one of the edge cases here.
Eric
On Mon, Oct 20, 2025 at 4:30 PM Phillip Brooks <phi...@um...> wrote:
> Let me try making my previous email a little more concise since it hasn't
> generated any follow-ups.
>
> 1) Is there a bust in the TIP 672 example code. I can't run this code
> from the TIP:
>
> set {} foobar ;# scalar variable
>
> set (index) "array value" ;# array variable
>
> When I run that, I get:
>
> can't set "(index)": variable isn't array
>
>
> Please explain.
>
> 2) The bash shell uses the syntax $(( expression )) for arithmetic. Does
> that syntax solve the problem of the unnamed array ambiguity that $(
> expression ) introduces? The $(( expression )) syntax, at least in my
> small example, seems illegal. I am only testing this out on a simple case
> I tried after the above code didn't work:
>
> % array set {} { 2+2 5 }
>
> % puts $(2+2)
>
> 5
>
>
> The above code illustrates the problem with $(2+2) being interpreted as an
> index into the unnamed array. If I try doing that with an extra ( in the
> expression, it is invalid syntax:
>
> % array set {} { (2+2) 5 }
>
> % puts $((2+2))
>
> invalid character in array index
>
>
> To get at the array element named (2+2), you have to add escape characters:
>
>
> % puts $(\(2+2\))
>
> 5
>
>
> It seems to me that if $(( expression )) works generally, it has several
> advantages:
>
> - it is easier to read and type than {=}{ expression }. (While {=}
> adds only two characters, { and } are shifted, = is unshifted which makes
> for much slower typing.)
> - it has precedent in bash
> - it doesn't overload the meaning of preexisting valid syntax.
>
> Are there other issues with $(( expression )) that I don't know of?
> Please explain?
>
> Phil
> _______________________________________________
> Tcl-Core mailing list
> Tcl...@li...
> https://lists.sourceforge.net/lists/listinfo/tcl-core
>
|