This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

call array from variable

Help for those learning Tcl or writing their own scripts.
Post Reply
M
Moonshine
Voice
Posts: 19
Joined: Sat Aug 09, 2003 4:19 pm

call array from variable

Post by Moonshine »

imagine u have an array like

Code: Select all

array set test1 {  a 1  
                          b 2  
                          c 3 }
now u can get the values like

Code: Select all

set result $test1(a)
but it doenst work if test1 is saved in a variable like:

Code: Select all

set whatever test1
set result $whatever(a)
why ?
what if i have the array name only in a variable ?

TIA
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

why would you want to do that?

if you need to pass the array as proc parameter, use [upvar]
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
S
StaleJoke
Voice
Posts: 10
Joined: Tue Nov 29, 2005 8:04 pm

Post by StaleJoke »

I have to set an array

Within the proc i want to use the variable like:

$users(1.#Channel1)

$users(2.#Channel1)


$users(1.#AnotherChannel2)

$users(2.#AnotherChannel2)

How should i go about doing it ?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

exactly as you did
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
S
StaleJoke
Voice
Posts: 10
Joined: Tue Nov 29, 2005 8:04 pm

Post by StaleJoke »

array set countem {}

Tcl error [countem]: can't read "users(1.#ChannelA)": no such element in array
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

array elements need to be initialized exactly like any other variable, too.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Post Reply