| View previous topic :: View next topic |
| Author |
Message |
Anansi Voice
Joined: 02 Jul 2007 Posts: 16
|
Posted: Tue Jul 17, 2007 2:02 pm Post subject: Is set? |
|
|
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. |
|
| Back to top |
|
 |
r0t3n Owner
Joined: 31 May 2005 Posts: 507 Location: UK
|
Posted: Tue Jul 17, 2007 3:20 pm Post subject: |
|
|
| Code: | 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: | | array exists arrayname - returns 1 if the array is set, or 0 if not |
_________________ r0t3n @ #r0t3n @ Quakenet |
|
| Back to top |
|
 |
Anansi Voice
Joined: 02 Jul 2007 Posts: 16
|
Posted: Tue Jul 17, 2007 4:24 pm Post subject: |
|
|
array exists was easy to find, but not info exists Thanks. |
|
| Back to top |
|
 |
Anansi Voice
Joined: 02 Jul 2007 Posts: 16
|
Posted: Tue Jul 17, 2007 4:54 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
awyeah Revered One

Joined: 26 Apr 2004 Posts: 1580 Location: Switzerland
|
Posted: Tue Jul 17, 2007 8:15 pm Post subject: |
|
|
You mean something like this:
| Code: |
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.
================================== |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jul 18, 2007 4:47 am Post subject: |
|
|
You could always create an empty array using this (this requires "myvar" to either be an array or not set at all.):
Or perhaps something like this to make sure the variable is unset prior initialization:
| Code: | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Anansi Voice
Joined: 02 Jul 2007 Posts: 16
|
Posted: Wed Jul 18, 2007 8:20 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jul 18, 2007 10:58 am Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
awyeah Revered One

Joined: 26 Apr 2004 Posts: 1580 Location: Switzerland
|
Posted: Wed Jul 18, 2007 12:18 pm Post subject: |
|
|
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.
================================== |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jul 18, 2007 2:01 pm Post subject: |
|
|
| 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|