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

incr $i ?

1 view
Skip to first unread message

USCode

unread,
Oct 12, 2008, 4:11:11 PM10/12/08
to
Given:

(Windows) 1 % incr i
1
(Windows) 2 % incr $i
1
(Windows) 3 % incr $$i
1
(Windows) 5 % puts $i
1
(Windows) 6 % puts $$i
$1

Shouldn't
incr $i or incr $$i
return an error from the Tcl interpreter as incr is looking for the var
name, not the variable?

Thanks!

USCode

unread,
Oct 12, 2008, 4:12:41 PM10/12/08
to
Sorry, this is with ActiveState Tcl 8.5.4 on Windows Vista.

Helmut Giese

unread,
Oct 12, 2008, 4:33:31 PM10/12/08
to
Hi,
for better or worse since Tcl 8.5 [incr] _creates_ the variable if it
doesn't already exist and assigns it the value 1.

>(Windows) 1 % incr i
>1
creates i with value 1

>(Windows) 2 % incr $i
>1
creates a variable with name '1'

>(Windows) 3 % incr $$i
>1
creates a variable with name '$1'

>(Windows) 5 % puts $i
>1
as expected

>(Windows) 6 % puts $$i
>$1
2 steps here:
- parser sees the first '$' and passes it on, since it is followed by
a second '$'
- parser then sees '$i' and performs substitution: $i -> 1
So [puts] sees '$1' which it then prints.

>Shouldn't
> incr $i or incr $$i
>return an error from the Tcl interpreter as incr is looking for the var
>name, not the variable?

See the initial comment.

Hint: Repeat your experiments from above and then do an 'info vars' -
you will find variables named '1', '$1', etc.
HTH
Helmut Giese

ZB

unread,
Oct 12, 2008, 6:16:31 PM10/12/08
to
Dnia 12.10.2008 USCode <do...@spamon.me> napisał/a:

> Shouldn't
> incr $i or incr $$i
> return an error from the Tcl interpreter as incr is looking for the var
> name, not the variable?

Take a look:

% set a 1
1
% set b a
a
% incr $b
2
% incr $b
3
% incr $b
4
% puts $b
a
% puts [set $b]
4

Now you know? ;)
--
ZB

USCode

unread,
Oct 12, 2008, 7:33:07 PM10/12/08
to
Helmut Giese wrote:
...

>
> Hint: Repeat your experiments from above and then do an 'info vars' -
> you will find variables named '1', '$1', etc.
> HTH
> Helmut Giese
Your explanation makes perfect sense, thanks.
I knew with 8.5 incr would now create the variable if it didn't exist
but I was surprised it allowed:
incr $i
I understand that is the substitution nature of Tcl, however I can see
that being a common mistake with the new behavior that might be missed.
0 new messages