Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-07-31
08:31 • Ticket [6ee2f5d5d6] Tcl 9.0 "invalid character in array index" error f... artifact: 2138b8f2cb user: oehhar
2026-07-30
19:37 • Ticket [6ee2f5d5d6]: 4 changes artifact: a4a550a21b user: avl42
18:48 • Ticket [6ee2f5d5d6]: 5 changes artifact: 21856fa4b0 user: avl42
18:45 • Ticket [6ee2f5d5d6]: 6 changes artifact: 164c01ff2e user: oehhar
15:23 • Ticket [6ee2f5d5d6]: 5 changes artifact: 4c023bfba7 user: avl42
10:26 • Ticket [6ee2f5d5d6]: 5 changes artifact: 368d335146 user: oehhar
10:25 • Ticket [6ee2f5d5d6]: 5 changes artifact: d83f786691 user: oehhar
10:01 • Ticket [6ee2f5d5d6]: 5 changes artifact: bdab65cc3e user: oehhar
09:48 • Ticket [6ee2f5d5d6]: 6 changes artifact: a2a07bc99f user: oehhar
09:25 • Closed ticket [6ee2f5d5d6]. artifact: ca6fd69dbe user: apnadkarni
09:11 • Ticket [6ee2f5d5d6]: 4 changes artifact: 65e121a697 user: avl42
09:04 • Ticket [6ee2f5d5d6]: 4 changes artifact: 26fdfe8b34 user: oehhar
02:37 • Ticket [6ee2f5d5d6]: 3 changes artifact: cf6af92bec user: apnadkarni
2026-07-29
20:20 • New ticket [6ee2f5d5d6]. artifact: 8b5636d366 user: jdelsign

Tcl 9.0 "invalid character in array index" error for $a("b")

Closed / Invalid6ee2f5d5d6bc7d697b735f5389d833ff9e61e217Created 126 days agoUpdated 125 days ago
Descriptionplain2026-07-29 20:20:21
I am in the process of upgrading from Tcl 8.6.14 to Tcl 9.0.4. The following commands produce an error in Tcl 9.0.4, but it was OK in Tcl 8.6.14.

$ tclsh
% set tcl_patchLevel
9.0.4
% set a("b") x
x
% array names a
{"b"}
% set a(b) y
y
% array names a
b {"b"}
% puts $a(b)
y
% puts $a("b")
invalid character in array index
% 

Why is that an error in Tcl 9.0.4?

The workaround I have been using is to access the value using set:

% set a("b")  
x
%
apnadkarnimarkdown2026-07-30 02:37:13

It appears this is a result of TIP 465. To quote,

Generally disallowing bare open parens, quotes and braces within array indices would mean that array indices on left hand side of an assignment could follow same rules as on right hand side

The motivation seems to have been TIP 282, which however has never come to a vote.

I wasn't privy to the original discussions so can't comment further. Leaving it to those who were to decide whether this is a bug or close as works-as-designed.

oehharwiki2026-07-30 09:04:34

Yes, special characters have to be escaped.

% set a("b") x
x
(bin) 2 % puts $a(\"b\")
x

I remeber a long discussion on this...

Perhaps, Andreas may comment on this.

And we may enhance the migration notes by examples.

Thanks for all, Harald

avl42 (claiming to be avl)plain2026-07-30 09:11:24
This is a necessary regression that had to happen at a major version shift, to pave the way for some extension in expr lateron.

The change does not affect setting of variables, but it affects specifically the syntax for retrieving array-elements.

In tcl 8 you could also do weird things like puts $a(((), if "((" was among the keys in the array; but no longer in Tcl 9.

Btw., the following variants continue to work:
# escaping the special characters, if they need to be given literally:
puts $a(\"b\")
set a((() z; puts $a(\(\()
# any chars can still be used via substitution:
set key {"b"}; puts $a($key)
foreach k [array names a] {puts "$k: $a($k)"}
apnadkarniplain2026-07-30 09:25:25
Thanks, @avl42, @oehhhar, closing then as works as designed.
oehharwiki2026-07-30 09:48:58

Perhaps we can enhance the text in the migration guide:

https://core.tcl-lang.org/tcl/wiki?name=Migrating+scripts+to+Tcl+9&p

For me, this is not cristal clear in the TIP or migration guide, as it only speaks about "{" and "(" and not about '"' and "\".

And it is not only about referencing:

% set a(\) x
x
% array names a
{}

So, in array creation, any trailing "\" was stripped from the index without an error. This is probably a bug but anyway.

In 9.0, the set causes an error.

oehharwiki2026-07-30 10:01:19

Single backslash in variable name resolution also workes with 8.6:

% set \\ x
x
% puts ${\}
x

On referencing with variables, a single "\" is taken as verbatim. On referencing with array indexes, a single "\" is stripped away.

And this is also interesting with 9.0.4:

% set \\ x
x
% puts ${\\}
can't read "\\": no such variable
% puts $\\
$\

So, a $\\ is verbatim interpreted - very interesting... There is no more verbatim way to express a variable name of "\"...

Thanks for all, Harald

oehharwiki2026-07-30 10:25:20

[e210f2ad2c] is proposed to enhance migration guide.

Thanks for all, Harald

oehharwiki2026-07-30 10:26:22

Grr, here is the correct changes link https://core.tcl-lang.org/tcl/wdiff?id=e210f2ad2cfea078

avl42plain2026-07-30 15:23:05
Hello again...

The syntax for ${...} is still VERY odd, and that wasn't
addressed/changed in that TIP, because it didn't cross the
plans for assignment in expr.  It is still as odd as it
was in 8.x, and it only becomes more odd, if you have
 puts ${\}  inside a braced block... try it... you'll be in
for some surprises, if you even try, unless you have really
really really understood the dodekalogue of Tcl syntax. ;-)

Regarding
set a(\) x
then the "set" command only sees "a()" and "x" as its arguments,
and never even saw the backslash there.
oehharwiki2026-07-30 18:45:48

Thanks, great. With the "set a(\) x" I just wanted to say, that this changed from 8.6 to 9.0. So it is not only referencing via dollar, but also setting.

Do you like my new migration notes?

THanks, Harald

avl42plain2026-07-30 18:48:20
Sorry, I've got to chime in again, but nothing changed in that case:

set a(\) x 

already did exactly the same thing in Tcl 8.x
avl42plain2026-07-30 19:37:23
And one more thing:   I also made a mistake in the comments below: I had totally forgotten that ${...} indeed also got sanitized (de-"odd"ed) between tcl-8.x and tcl 9.0. Sorry about that.

I've also had a look at your(oehhar) changes:  yes, they're fine and correct, except maybe for the last line regarding symmetry to expr...  symmetry is not the thing here...   Discussion back then came to the result that those
tcl-8's odd-balls regarding $arr(...) and ${...} would just spoil all
attempts at making something like [expr {a(((()=42+12}] a consistent parse.
Maybe "consistency" would be a preferred wording w.r.t the changes and "expr".
oehharwiki2026-07-31 08:31:59

Ok, great. symetry->consistency changed. Thanks for all, Harald

Log in (anonymous is fine) to add a comment.