question

takabanana avatar image
takabanana asked udaya edited

Using Variables for dynamic Variable Names

Please see attached images... 1. I have local variables called $client1_ip, $client2_ip, $client3_ip, $cliept4_ip 2. I am trying to loop, using i as a varaible, and using each of those variables. Why is the way I am doing it not working? This is probably mostly a Tcl question... And YES I know I could have used an array of variables - but let's not go there.. :-) Just trying to figure THIS out with a dynamic variable name for now... :-) ![alt text][1] The 3 error(s) I get for the above are: ![alt text][2] Any clues on how to implement this correctly? [1]: /storage/attachments/124-2011-12-08_110120.png [2]: /storage/attachments/125-2011-12-08_110336.png
iTesttcl scriptVariables
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mallikarjunarao avatar image
mallikarjunarao answered mallikarjunarao posted

Hi takabanana,

 

 

eval $[concat client$i\_ip]

 

3 comments
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

udaya avatar image udaya commented ·

Hi Malli,

 

I deleted my earlier one as I was out of channel on that.

 

The above suggestion id not gonna work.

 

When i value would be 1 the result would be

 

$client1_ip
  But it would not show the desired value

0 Likes 0 ·
mallikarjunarao avatar image mallikarjunarao udaya commented ·

Hi Uday,

 

Try this its woking fine, if you facing any issues please let me know.

 

 

set client1_ip "192.168.100.1"
set client2_ip "192.168.100.2"
set client3_ip "192.168.100.3"
set client4_ip "192.168.100.4"
set client5_ip "192.168.100.5"
set client6_ip "192.168.100.6"
for {set i 1} {$i < 6} {incr i} {
	eval puts $[concat client$i\_ip]
}

 

0 Likes 0 ·
udaya avatar image udaya mallikarjunarao commented ·

Sorry, I missed a command.

 

I was playing with

puts $[concat client$i\_ip]

 I should try

eval puts $[concat client$i\_ip]

 Got it.

0 Likes 0 ·
AmishP avatar image
AmishP answered udaya edited

Hi,

Try [subst \$client[subst $i]_ip]

First subst will subst value of i and second will for the whole variable.

-A

10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.