Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.tcl

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-30-2012, 09:43 AM
Shimsha
Guest
 
Posts: n/a
Default Getting the value without direct usage of the variable

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?
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-30-2012, 12:46 PM
Gerald W. Lester
Guest
 
Posts: n/a
Default Re: Getting the value without direct usage of the variable

On 7/30/12 4:43 AM, Shimsha wrote:
> 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?


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.Lester@kng-consulting.net |
+------------------------------------------------------------------------+
Reply With Quote
  #3 (permalink)  
Old 07-30-2012, 12:46 PM
Gerald W. Lester
Guest
 
Posts: n/a
Default Re: Getting the value without direct usage of the variable

On 7/30/12 4:43 AM, Shimsha wrote:
> 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?


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.Lester@kng-consulting.net |
+------------------------------------------------------------------------+
Reply With Quote
  #4 (permalink)  
Old 07-30-2012, 01:11 PM
Charlie Bursell
Guest
 
Posts: n/a
Default Re: Getting the value without direct usage of the variable

On Monday, July 30, 2012 4:43:48 AM UTC-5, Shimsha wrote:
> 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?


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
Reply With Quote
  #5 (permalink)  
Old 07-30-2012, 01:35 PM
Frederic Bonnet
Guest
 
Posts: n/a
Default Re: Getting the value without direct usage of the variable

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.

Reply With Quote
  #6 (permalink)  
Old 07-30-2012, 03:43 PM
Ralf Fassel
Guest
 
Posts: n/a
Default Re: Getting the value without direct usage of the variable

* Frederic Bonnet <fredericbonnet@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'
Reply With Quote
  #7 (permalink)  
Old 07-30-2012, 03:51 PM
Frederic Bonnet
Guest
 
Posts: n/a
Default Re: Getting the value without direct usage of the variable

Le 30/07/2012 17:43, Ralf Fassel a écrit :
> * Frederic Bonnet <fredericbonnet@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'
>

Oops you're right, my bad. :blush:

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:16 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.