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.

Problem with arrays

Help for those learning Tcl or writing their own scripts.
Post Reply
N
Nara
Halfop
Posts: 40
Joined: Sun Jul 23, 2006 11:12 pm

Problem with arrays

Post by Nara »

Alright, here's what I got:

Code: Select all

bind notc - * battle:noticekick
bind pubm - * battle:mainchan
bind time - "00 * * * *" battle:unsetwarn

array set warned {}


proc battle:noticekick {nick uhost handle text dest} {
	if {[validchan $dest]} {
		set dest [string tolower $dest]
		if {[string equal $dest "#century0"] && ![matchattr $handle D]} {
			putquick "PRIVMSG Chanserv :#century0 addtimedban $nick 3m Please for the love all that’s holy and rocky don’t do that. Trust us if we want the channel to make a large beeping sound…..we can do it ourselves. \[3 minute ban\]"
		}
	}
}

proc battle:warnnick {nick host hand chan text} {
	putserv "NOTICE $nick :You almost stepped on a landmine there Mr. Rockstar. Check out @rockrules and @advertising"
}


proc battle:mainchan {nick host hand chan text} {
	global warned
	set chan [string tolower $chan]
	if {[string equal $chan "#century0"]} {
		set text [stripcodes bcr $text]
			if {[string match -nocase "*hotrockplanet*" $text] || [string match -nocase "*egln*" $text]} {
				return 0
			} elseif {[string match -nocase "*#*" $text] || [string match -nocase "*www*" $text] || [string match -nocase "*.com*" $text] || [string match -nocase "*.net*" $text] || [string match -nocase "*.org*" $text] || [string match -nocase "*http*" $text]} {
				if {$warned($nick) == 1} {
					putserv "PRIVMSG Chanserv :#century0 addtimedban $nick 3m Advertising is not permitted in #hotrockplanet. Please read @advertising and @rockrules when you return. \[3 minute ban\]"
					set warned($nick) 0
				} else {
					set warned($nick) 1
					battle:warnnick nick host hand chan text
					}
			} else {
				return 0
			}
	}
}

proc battle:unsetwarn { min hour day month year } {
	global warned
 	foreach nick [array names warned] {
 		if {$warned($nick) == 1} {
   		set $warned($nick) 0
  		}
	}
}
Here's the error:

Code: Select all

[19:59] Tcl error [battle:mainchan]: can't read "warned(HRock|Darin)": no such element in array
~Nara
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

N
Nara
Halfop
Posts: 40
Joined: Sun Jul 23, 2006 11:12 pm

Post by Nara »

Besides the fact it's probably an obvious method, I did follow that topic unless you're talking about .set errorInfo?

If so:

Code: Select all

[22:18] #Century0# set errorInfo
Currently: can't read "warned(Ging_sleepTIME)": no such element in array
Currently:     while executing
Currently: "if {$warned($nick) == 1} {
Currently:                                      putserv "PRIVMSG Chanserv :#cent                                              ury0 addtimedban $nick 3m Advertising is not permitted in #hotrockplanet. Please                                               read ..."
Currently:     (procedure "battle:mainchan" line 9)
Currently:     invoked from within
Currently: "battle:mainchan $_pubm1 $_pubm2 $_pubm3 $_pubm4 $_pubm5"
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Code: Select all

if {$warned($nick) == 1} {
               putserv "PRIVMSG Chanserv :#century0 addtimedban $nick 3m Advertising is not permitted in #hotrockplanet. Please read @advertising and @rockrules when you return. \[3 minute ban\]"
               set warned($nick) 0 
You test the var before ever setting it elsewhere (line 9 of the proc)..
N
Nara
Halfop
Posts: 40
Joined: Sun Jul 23, 2006 11:12 pm

Post by Nara »

Well, can you help me come up with a way to initialize the variable, because I can't just set it to a value because it could be 1 or 0.
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

You have to at least init the var with the nick, so somewhere you need to do like: set warned($nick) ""
N
Nara
Halfop
Posts: 40
Joined: Sun Jul 23, 2006 11:12 pm

Post by Nara »

What's the command to see names in a chan, preferably one I could use like [foreach nick chan]?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

look here for "chanlist"
N
Nara
Halfop
Posts: 40
Joined: Sun Jul 23, 2006 11:12 pm

Post by Nara »

Alright, this is my solution, but sadly, it doesn't work for the main part (if $nick == $botnick, then find all nicks in chan and set them to 0.

Code: Select all

bind join - "#century0 *" battle:setnicks

array set warned {}

proc battle:setnicks {nick uhost handle chan} {
	global warned botnick
	if {$nick == $botnick} {
		foreach nick [chanlist #century0] {
			set warned($nick) 0
			}
	} else {
		set warned($nick) 0
	}
}
Could it be because the code is executing before the bot has got the list or did I just do it wrong?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

You're testing {$nick == $botnick} first, so of course the part where the array is being set, is not going to be set unless the nick is the botnick..

You probably want to do {foreach nick [chanlist #century0]} {if {$nick != $botnick} and so on, so that the arrays are set for each nick except botnick...But, actually, that will only work on existing names in the channel, and not when they join/part/quit. It would be more reasonable to test for some condition that you're trying to warn for, set the array var for the nick, then execute whatever warning/punishment..

The logic and intention of your original script is not very clear, so it's difficult to make suggestions..
r
r0t3n
Owner
Posts: 507
Joined: Tue May 31, 2005 6:56 pm
Location: UK

Post by r0t3n »

Instead of a foreach loop to catch all the users on the channel and setting them in an array, why not check if the variable for the user exists then set it if not.

For example:

Code: Select all

proc battle:mainchan {nick host hand chan text} {
   global warned
   set chan [string tolower $chan]
   if {[string equal $chan "#century0"]} {
     if {![info exists warned($nick)]} {
       set warned($nick) "0"
       return
     }
     ..... rest of code here ......
} 
Also maybe put all of those string match's into one simple regexp.

Hope this helps..!
r0t3n @ #r0t3n @ Quakenet
Post Reply