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.

How to check for a valid variable without getting an error

Help for those learning Tcl or writing their own scripts.
Post Reply
A
A Hylian Human
Voice
Posts: 5
Joined: Mon Sep 06, 2010 11:59 am

How to check for a valid variable without getting an error

Post by A Hylian Human »

Code: Select all


namespace eval rules {
 set #Zelda("nick") "There is no need to spam nick changes; pick a nick and keep it! If you are in #RolePlay and need to change nicks frequently, please lea$
 set #Zelda("flood") "Please do not flood (putting several lines of text into the channel at once) or spam (posting useless/irrelevant information). Consecu$
 set #Zelda("ads") "Do not advertise any channels or servers. Do not advertise any TZCN channels on any other channels or servers."
 set #Zelda("fights") "We ask that any arguments or fights be taken to another channel or private message."
 set #Zelda("beg") "Do not beg for ops or moderator status. This includes attempting to use operator commands (including but not limited to !op, !up, !down,$
 set #Zelda("mature") "Do not post grotesque or vulgar content. This includes, but is not limited to, shock sites, pornography, and explicit literature (ero$
 set #Zelda("profanity") "Minor profanity is allowed. However, excessive swearing and discriminatory/racial slurs will be dealt with harshly."
 set #Zelda("scripts") "While scripts can be humorous and fun, we ask that flood controls be implemented. Otherwise, your script will cause flooding, which $
 set #Zelda("roleplay") "This channel (#Zelda) is meant for general discussion, not roleplaying. If you want to roleplay, join #RolePlay."
 set #Zelda("nice") "The most important rule: be nice. Do not harass others be needlessly highlighting them (saying there name/nick). If they don't want to $
 set #Zelda("agreement") "As with most communities, this chat room has rules. We (the moderators of this chat room) expect you to follow them at all times. $
 set #Zelda("overview") "An overview of the rules: do not flood; do not post large amounts of text into the chat room, change nicks frequently, or rejoin th$
 set #Zelda("unknown") "Unknown rule option. Syntax: !rule <option>. The options are:"
 set #Zelda("options") [list "nick" "flood" "ads" "fights" "beg" "mature" "profanity" "scripts" "roleplay" "nice" "agreement" "overview" "default" "options"]

 set #Hyrule_Castle("agreement") "You must keep this channel away from prying eyes (non-staff are not welcomed here)."
 set #Hyrule_Castle("overview") "This is TZCN's Staff Channel. If you are not staff, get out. Otherwise, no rules. :)"
 set #Hyrule_Castle("unknown") "Hey! There aren't any rules, okay!? Two options:"
 set #Hyrule_Castle("options") [list "overview" "agreement"]

 bind pub -|- "!rules" rules::showrules

 proc showrules {nick uhost hand chan args} {
  if { [set [subst $chan]("options")] == "" } { putnotc $nick "I do not know the rules for $chan. Be sure to ask the moderators." }
  elseif {[lsearch $arg [set [subst $chan]("options")] != -1} { putnotc $nick [set [subst $chan]("options")] }
  else {
   join opts ", "
   putnotc $nick "[set [subst $chan](unknown)] [set [subst $chan](options)]."
   putnotc $nick "[set [subst $chan](agreement)]"
  }
 }
}
I'm trying to create a rules script. I can't seem to get it to work and my only solution (above) is turning out to be more complex than I had hoped.

Can someone help me?

I get this
Tcl error [rules::showrules]: can't read "#Hyrule_Castle("options")": no such variable
whenever I type
!rules
I just don't know what to do. :|
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Re: How to check for a valid variable without getting an err

Post by willyw »

Hello,

Regarding the topic of:
How to check for a valid variable without getting an error


How about this? :
[info exists variable]

reference: http://www.tcl.tk/man/tcl8.5/TclCmd/info.htm#M11

Could you use that, in an "if" statement, to do your checking?





[quote="A Hylian Human"]

Code: Select all

...
I'm trying to create a rules script. I can't seem to get it to work and my only solution (above) is turning out to be more complex than I had hoped.
...
[/quote]


As for this being the "only" solution...  ...   here's what I do, for what it may be worth -
I use this script:
holm-news.tcl 	4.0 	Displays the contents of a news file when a trigger is used.
found here:
http://www.egghelp.org/tclhtml/3478-4-0-0-1-holm-news.htm

to display channel rules.
I do change the trigger word.

All it does, is read a text file and display it.

Edit the script, and change the trigger to suit yourself,  create a text file with your rules in it,  and you are in business.    :)



I hope one of these ideas helps.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Re: How to check for a valid variable without getting an err

Post by speechles »

A Hylian Human wrote:I'm trying to create a rules script. I can't seem to get it to work and my only solution (above) is turning out to be more complex than I had hoped.

Can someone help me?

I get this
Tcl error [rules::showrules]: can't read "#Hyrule_Castle("options")": no such variable
whenever I type
!rules
I just don't know what to do. :|
The problem is very simple. You forgot to link your "global" variables to the "local" scope of your procedure. I've corrected your procedure below and commented the single line I added, and you had some line with ( join opts ", " ) which is not anything resembling tcl..

Code: Select all

 proc showrules {nick uhost hand chan args} {
  # here is the line i've added below
  global #Zelda #Hyrule_Castle
  if { [set [subst $chan]("options")] == "" } {
    putnotc $nick "I do not know the rules for $chan. Be sure to ask the moderators."
  } elseif {[lsearch $arg [set [subst $chan]("options")] != -1} {
    putnotc $nick [set [subst $chan]("options")]
  } else {
   putnotc $nick "[set [subst $chan](unknown)] [join [set ::[subst $chan](options)] ", "]."
   putnotc $nick "[set [subst $chan](agreement)]"
  }
 }
} 
Not sure really wtf this is supposed to do. So i've only corrected tcl errors, not any involving logic and such.
A
A Hylian Human
Voice
Posts: 5
Joined: Mon Sep 06, 2010 11:59 am

Post by A Hylian Human »

Yeah I dunno what I was thinking.
But I came up with an idea that I think might work.
I'll post it tomorrow.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Since the variables are within a given namespace ("rules"), using global as suggested by speechles is incorrect. Instead, you should either use fully qualified namespace paths, or the "variable" command to link the variable from the current namespace into your proc.

For reading arrays, I'd suggest using the "array" command such as this:

Code: Select all

...
somecommand [array get "::rules::$chan" options]
...
NML_375
Post Reply