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

Getting the value without direct usage of the variable

51 views
Skip to first unread message

Shimsha

unread,
Jul 30, 2012, 5:43:48 AM7/30/12
to
set a b
set b 7

Now I have to print the value of a as 7. But should not use b or any other new variable anywhere. How could this be achieved?

Gerald W. Lester

unread,
Jul 30, 2012, 8:46:53 AM7/30/12
to
First off, the way you are approaching the problem is very likely wrong --
consider using arrays instead.

If you insist on going about your code this way, read and understand the
following man pages:
set
subst
upvar



--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

Gerald W. Lester

unread,
Jul 30, 2012, 8:46:53 AM7/30/12
to
On 7/30/12 4:43 AM, Shimsha wrote:

Charlie Bursell

unread,
Jul 30, 2012, 9:11:45 AM7/30/12
to
I don't consider it wrong. I use indirection such as this a lot. The simple answer is: puts [set $a]

Of course as mentioned there are other methods as well

Frederic Bonnet

unread,
Jul 30, 2012, 9:35:38 AM7/30/12
to
Le 30/07/2012 11:43, Shimsha a �crit :
> set a b
> set b 7
>
> Now I have to print the value of a as 7. But should not use b or any other new variable anywhere. How could this be achieved?

If your goal is to define a as a reference or alias to b, upvar is what
you're looking for:

upvar #0 a b
set b 7
puts $a
=> 7

(the #0 means that a and b must be in the same context)

Else, you have to be a little more explicit about what you intent to
achieve.

Ralf Fassel

unread,
Jul 30, 2012, 11:43:59 AM7/30/12
to
* Frederic Bonnet <frederi...@free.fr>
| upvar #0 a b
| set b 7
| puts $a
| => 7
>
| (the #0 means that a and b must be in the same context)

I *think* that should be plain 0 here, since #0 addresses the global
namespace. Makes a difference inside procs.

R'

Frederic Bonnet

unread,
Jul 30, 2012, 11:51:04 AM7/30/12
to
Le 30/07/2012 17:43, Ralf Fassel a �crit :
Oops you're right, my bad. :blush:

0 new messages