For Programmers: Free Programming Magazines  


Home > Archive > Tcl > June 2007 > How to implement pointer operation









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to implement pointer operation
prabu

2007-06-28, 7:12 pm

Hi,

This may be a simple one. Consider the following.

set a 10
set b $a

Now I want to print the value of "a" using the variable "b". How can I
do this?

Thanks in Advance,
Prabu.

Ron Fox

2007-06-28, 7:12 pm

prabu wrote:
> Hi,
>
> This may be a simple one. Consider the following.
>
> set a 10
> set b $a
>
> Now I want to print the value of "a" using the variable "b". How can I
> do this?
>
> Thanks in Advance,
> Prabu.
>



You want something like this:

set a 10
set b a; # B 'points' to a

puts [set $b]

In the puts command, first $b is substituted leaving [set a]
Then the []'s are done and the result of [set a] is the contents
of the variable a.. or 10.

Ron.
Donal K. Fellows

2007-06-28, 7:12 pm

Ron Fox wrote:
> You want something like this:
>
> set a 10
> set b a; # B 'points' to a
> puts [set $b]


The other thing is that almost all code that does this a lot is better
written using arrays.

set ary(a) 10
set b a
puts $ary($b)

Donal.
prabu

2007-06-29, 8:08 am

On Jun 28, 8:26 pm, "Donal K. Fellows"
<donal.k.fell...@manchester.ac.uk> wrote:
> Ron Fox wrote:
>
>
> The other thing is that almost all code that does this a lot is better
> written using arrays.
>
> set ary(a) 10
> set b a
> puts $ary($b)
>
> Donal.


Thanks guys

Tobias Hippler

2007-06-29, 8:08 am

prabu wrote:
> Hi,
>
> This may be a simple one. Consider the following.
>
> set a 10
> set b $a
>
> Now I want to print the value of "a" using the variable "b". How can I
> do this?
>
> Thanks in Advance,
> Prabu.
>


Hi,

in addition to the already proposed solutions, you can always use
"upvar" to create sort of references to variables:

set a 10
upvar 0 a b

set b 17
puts $a; # output: 17

Tobi.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com