Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bug or feature?

76 views
Skip to first unread message

rene

unread,
Aug 7, 2017, 11:15:08 AM8/7/17
to
Hi all,

why is in tcl8.6.5 "?" a valid array name but $?(..) doesn't work?

(data)1% array set ? {a 1 b 2 c 3}
(data)2% array names ?
a b c
(data)3% set ?(a)
1
(data)4% puts $?(a)
$?(a)
(data)5% puts ${?}(a)
can't read "?": variable is array
(data)6% puts ${?(a)}
1


Regards
rene

Rich

unread,
Aug 7, 2017, 11:35:22 AM8/7/17
to
rene <r.zau...@freenet.de> wrote:
> Hi all,
>
> why is in tcl8.6.5 "?"

Because Tcl allows anything to be a variable name.

> a valid array name but $?(..) doesn't work?

It does work, you just did not quite do things correctly below.

>
> (data)1% array set ? {a 1 b 2 c 3}
> (data)2% array names ?
> a b c
> (data)3% set ?(a)
> 1
> (data)4% puts $?(a)
> $?(a)
> (data)5% puts ${?}(a)
> can't read "?": variable is array
> (data)6% puts ${?(a)}
> 1

Because when using the ${} notation to access anything you have to put
the "whole name" inside the curly brackets:

$ rlwrap tclsh
% array set ? {a 1 b 2 c 3}
% array names ?
a b c
% set ?(a)
1
% puts ${?(a)}
1
%

Robert Heller

unread,
Aug 7, 2017, 12:18:47 PM8/7/17
to
At Mon, 7 Aug 2017 15:31:02 -0000 (UTC) Rich <ri...@example.invalid> wrote:

>
> rene <r.zau...@freenet.de> wrote:
> > Hi all,
> >
> > why is in tcl8.6.5 "?"
>
> Because Tcl allows anything to be a variable name.
>
> > a valid array name but $?(..) doesn't work?
>
> It does work, you just did not quite do things correctly below.
>
> >
> > (data)1% array set ? {a 1 b 2 c 3}
> > (data)2% array names ?
> > a b c
> > (data)3% set ?(a)
> > 1
> > (data)4% puts $?(a)
> > $?(a)
> > (data)5% puts ${?}(a)
> > can't read "?": variable is array

Right. This syntax allows something like this:

set foo ?
puts [set ${foo}(a)]

And that is one of the major uses of the ${...} syntax -- *computed* variable
names in ambigious contexts.

> > (data)6% puts ${?(a)}
> > 1
>
> Because when using the ${} notation to access anything you have to put
> the "whole name" inside the curly brackets:
>
> $ rlwrap tclsh
> % array set ? {a 1 b 2 c 3}
> % array names ?
> a b c
> % set ?(a)
> 1
> % puts ${?(a)}
> 1
> %
>
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Donal K. Fellows

unread,
Aug 8, 2017, 3:45:04 PM8/8/17
to
On 07/08/2017 16:15, rene wrote:
> why is in tcl8.6.5 "?" a valid array name but $?(..) doesn't work?

Because the brace-free form of $ doesn't syntactically support “?” (or
any number of other characters, such as π or Ω or «») as a variable
name; variables actually support more than $ in their names. The
restriction is not usually a problem, though I admit I'd quite like to
have Greek alphabetics when doing something fairly mathematical. :-)

The braced form of $ requires the braces to go round the whole variable
name, including the array element part.

These are all purely syntactic restrictions that stem from trade-offs in
parsing of your script. The underlying variable access APIs can accept
most characters as variable names, subject to the restriction that a
sequence of two-or-more colons is special and parentheses really should
only be used for indicating array name/element name separation.

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.
0 new messages