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.

Is set?

Help for those learning Tcl or writing their own scripts.
Post Reply
A
Anansi
Voice
Posts: 16
Joined: Mon Jul 02, 2007 8:03 am

Is set?

Post by Anansi »

Is there a way to return (without crashing the procedure) whether a variable is set or not set? I need this specifically for array elements, but it would also be useful (and work in a similar manner) for normal variables.

I looked in the Tcl manual and could find nothing.
r
r0t3n
Owner
Posts: 507
Joined: Tue May 31, 2005 6:56 pm
Location: UK

Post by r0t3n »

Code: Select all

info exists varname - returns 1 if set, or 0 if not

info exists arrayname(arrayelement) - returns 1 if set, or 0 if not
To see if an array is set:

Code: Select all

array exists arrayname - returns 1 if the array is set, or 0 if not
r0t3n @ #r0t3n @ Quakenet
A
Anansi
Voice
Posts: 16
Joined: Mon Jul 02, 2007 8:03 am

Post by Anansi »

array exists was easy to find, but not info exists :P Thanks.
A
Anansi
Voice
Posts: 16
Joined: Mon Jul 02, 2007 8:03 am

Post by Anansi »

Can I declare a variable as an array without actually writing anything on it? I want a global array to be initialized within a procedure, but if I don't initialize it outside the procedure when I try to initialize it inside it returns the "not an array" error. I'm using "global arrayname".

Ok, nevermind. Rehash just didn't reset the variables properly. A restart solved it.
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

You mean something like this:

Code: Select all

proc name {arg1 arg2 arg3} {
 global myarray
 if {[info exists myarray($arg1)]} {
  #whatever you want to do here
 }
}
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

You could always create an empty array using this (this requires "myvar" to either be an array or not set at all.):

Code: Select all

array set myvar ""
Or perhaps something like this to make sure the variable is unset prior initialization:

Code: Select all

unset myvar
array set myvar ""
Also keep in mind, that the global-command really does'nt create, alter or otherwize directly affect any variable. All it does is link the local variable (within a proc) to the corresponding globalspace variable.
NML_375
A
Anansi
Voice
Posts: 16
Joined: Mon Jul 02, 2007 8:03 am

Post by Anansi »

Right, but it was bitching about the previously untouched variable "not being an array" when I tried to set one of its members. unsetting the variable just prior to setting it was not an option because the procedure that set the variable was called several times to set different members - unsetting the variable before setting it would mean it would never have more than one member. Fortunately, it was just a problem of eggdrop not reseting the variables well.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Maybe I should've been alittle clearer, the code above was intended to initialize the variable upon reset/rehash, and not be used within various procs ("array set" will overwrite any existing data in the array).
NML_375
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Whats the use of setting an empty array and then unsetting it anyway?
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

awyeah wrote:Whats the use of setting an empty array and then unsetting it anyway?
None to my knowledge..
There is, however, a point in unsetting it and then creating an empty array
NML_375
Post Reply