egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Is set?

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Anansi
Voice


Joined: 02 Jul 2007
Posts: 16

PostPosted: Tue Jul 17, 2007 2:02 pm    Post subject: Is set? Reply with quote

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
View user's profile Send private message
r0t3n
Owner


Joined: 31 May 2005
Posts: 507
Location: UK

PostPosted: Tue Jul 17, 2007 3:20 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Anansi
Voice


Joined: 02 Jul 2007
Posts: 16

PostPosted: Tue Jul 17, 2007 4:24 pm    Post subject: Reply with quote

array exists was easy to find, but not info exists Razz Thanks.
Back to top
View user's profile Send private message
Anansi
Voice


Joined: 02 Jul 2007
Posts: 16

PostPosted: Tue Jul 17, 2007 4:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Tue Jul 17, 2007 8:15 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jul 18, 2007 4:47 am    Post subject: Reply with quote

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

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
View user's profile Send private message
Anansi
Voice


Joined: 02 Jul 2007
Posts: 16

PostPosted: Wed Jul 18, 2007 8:20 am    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jul 18, 2007 10:58 am    Post subject: Reply with quote

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
View user's profile Send private message
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Wed Jul 18, 2007 12:18 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jul 18, 2007 2:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber