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.

String matching

Old posts that have not been replied to for several years.
Locked
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

String matching

Post by Damnation85 »

Hello once again....

i am coding a adduser code for my bot and the commands being set of levels

ie 100
250
etc etc

but i also want to assign these levels to wordss like chanvoice etc

i want to know whats the most efficient way of doing this so when a staff member types

/msg <botnick> adduser <chan> <user> <host> chanvoice

it adds the user to the bot with the predefined level that chanvoice matches.

also i want to know this as i plan to do a trivia extension to my bot and i want it to match the answers.

i heard of regexp should i use that?
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: String matching

Post by user »

I don't see the connection...what's the translation of the level name to level number got in common with matching answers in a trivia script?
For the level thing:

Code: Select all

array set s2i {chanvoice 100 chanop 200}
# let's pretend you've got the level name from the request stored in 'lev'...
if {[info exists s2i($lev)]} {
  # valid level name... $s2i($lev) is the number
} else {
  # invalid (unknown) level name
}
Answer matching:

Code: Select all

if {[string equal -nocase $theAnswer $userInput]} {
  # correct
} else {
  # wrong
}
EDIT: code typo
Last edited by user on Fri Jan 07, 2005 2:51 pm, edited 1 time in total.
Have you ever read "The Manual"?
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

Post by Damnation85 »

nothing really i sorta worked out the answer to the string/integ problem as i wrote the post.

although your method looks cleaner than mine i think i might toy with that a little thank you for that example

and
thank you for the trivia part they both make perfect sense. i am quite enjoying coding in tcl. very straight forward. quite nice to code.

i'd be expecting more posts from a lil old newbie like me :o)

thanx for all your help so far. to all of you.
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

Post by Damnation85 »

i don't know if this was the correct way but it works at least.

Code: Select all

	 set level [lindex $arg 3]
	 
	 if {[lindex $arg 0] == "" || [lindex $arg 1] == "" || [lindex $arg 2] == "" || [lindex $arg 3] == ""} {
	 	 puthelp "NOTICE $nick :Format: /msg <BOTNICK> ADDUSER \00315<#CHANNEL> \00312<USER> \00303<HOST> \00307<LEVEL>\003"
	 	 puthelp "NOTICE $nick :Example: /msg $botnick ADDUSER\00315 $chan \00312Damnation \00303Damnation@The-Damned.net \003071000\003"
	 	 return 0
	 }
 
	 array set matches {chanvoice chanvoice chanop chanop chanmaster chanmaster chanowner chanowner}
	 
	 if {[info exists matches($level)]} {
	 	 puthelp "NOTICE $nick :Its: $svbot($matches($level))"
	 } else {
	 	 puthelp "NOTICE $nick :ERROR: Invaild command level"
	 	 return 0
	 }
the ||'s in the ifs is there a more efficient way to do that ?

and the snippet you left works wonders (theres a set svbot(chanvoice) etc etc at the top of the code as a global)

i was just wonder ok so the user puts in the word but whatif the user places in a integer. is there a check for strings/integers? (looks at docs to see and patiently awaits a answer)
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Where/how is the list $arg created?
To check for integers, use 'string is int $level' (since you check if it's empty first you don't need the -strict option)
Have you ever read "The Manual"?
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

Post by Damnation85 »

will try what u just stated in 1 sec the arg is created via 2 ways

public command

adduser user host level

or msg

adduser chan user host level

although i asked a stupid damn question just not i don't need to check the if integer or not i just need to let it through on the integer and if its a string to make sure it matches one of the elements in the array if it doesn't send a error output to the user then return.

or at least thats what i think....

would like your input tho
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

Post by Damnation85 »

Code: Select all

	 if { ![string is int $level] } {
	 	 array set matches {chanvoice chanvoice chanop chanop chanmaster chanmaster chanowner chanowner}
	 	 if {[info exists matches($level)]} {
	 	 	 puthelp "NOTICE $nick :Its: $svbot($matches($level))"
	 	 	 set level $svbot($matches($level))
	 	 	 puthelp "NOTICE $nick :Its: $level"
	 	 	 return 0
	 	 } else {
	 	 	 puthelp "NOTICE $nick :ERROR: Invalid command level"
	 	 	 return 0
	 	 }
	 }
that seems to work, is there a better way tho?
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

'arg' contains the unmodified user input at this point...

Code: Select all

if {[scan $arg %s%s%s%s a b c d]!=4} {error}
or

Code: Select all

set arg [split $arg]
if {[llength $arg]<4} {error}
foreach {a b c d} $arg break;# creates the four variables (to match the result of the previous example)
Your expression with the ||'s could be improved by reversing it (as the first word is always empty when the last one is etc.. :)
What's the purpose of your matches array? Why don't you use svbot directly?

EDIT: i need some rest :P screwed up the order of arguments in the scan :P
Last edited by user on Fri Jan 07, 2005 4:10 pm, edited 2 times in total.
Have you ever read "The Manual"?
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

Post by Damnation85 »

*looks at answer with boggled eyes* ok i think i'll have a play with that so i learn to understand it and yes i can see what your point is there on the ||'s only really need the one because if there isn't 3 options typed or even 4 options
it will always fail, but still need to check the others for stuff in them hehe nice one for pointing that out.
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

I didn't notice that you accept 3 OR 4 args untill now...here's an idea for you:

Code: Select all

switch [scan $arg %s%s%s%s 0 1 2 3] {
	4 {
		# have four non-empty arguments ($0, $1, $2 and $3)
	}
	3 {
		# three...which is also okay ($0, $1 and $2)
	}
	default {
		# too few...bitch and whine
	}
}
Have you ever read "The Manual"?
D
Damnation85
Voice
Posts: 19
Joined: Sun Dec 19, 2004 2:31 pm

Post by Damnation85 »

its k, the 3 options is in the public comand which then passes its varibles to the msg command so that i don't duplicate code.the 4 check is in the msg code (chan being one of the checks)

thanx for your help i am slowly tackling your snippets and making them work for me, and learning to understand them, it is greatly reducing the number of lines of code that the proc is using
Locked