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.

lsearch and list

Help for those learning Tcl or writing their own scripts.
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

lsearch and list

Post by Craig »

How to do lsearch for two or more words? like:

[lsearch -all -inline {1 11 111 22 2 222} 2* 1*]

i want to if find me words with 2* 1* .. so 1 11 111 2 22 222

[lsearch -all -inline {147 158 169 175 121 540} *0 *9]

return => 540 169


------

how to do list?

like i got set $word "word1 2 3"

i want to it be :

word1
2
3
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Checking the manpage for lsearch, you'd see it only supports one pattern.
However, it does support various matching-algorithms, including regular expressions ("regexps"). Regular expressions do allow you to make quite complicated patterns, including matching multiple keywords.

Ex:

Code: Select all

lsearch -regexp -all -inline {1 11 111 22 2 222} {^(1|2).*}
As for your second inquery, if you're using a string, I guess you could do something like this:

Code: Select all

join [split $word] "\n"
NML_375
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

could u "repair" my part of script ?

Code: Select all

	set readbw [open [string trimleft $chan #]_badwords.txt r] 
	while { ![eof $readbw] } {  
	putlog "i do something"
	gets $readbw line 
	set helpwanted [string range $line 0 [expr [string first # $line]-1]] 
	set message [string range $line [expr [string first # $line]+1] [expr [string first ź $line]-1]] 
	set typeof  [string range $line [expr [string first ź $line]+1] end] 
if { ([string match -nocase "* $helpwanted *" $input] == 1 && $helpwanted != "") || ([string match -nocase "$helpwanted" $input] == 1 && $helpwanted != "") } {
		if { $typeof == 3 } {
			putquick "PRivmsg $chan : cos znalazlem! :D"
			set Excepts(File) [open excepts_[string trimleft $chan #]_badwords.txt r] 
			while { ![eof $Excepts(File)] } {  
				gets $Excepts(File) line
				set Excepts(list) [string range $line 0 [expr [string first # $line]-1]]
				close $Excepts(File)
				## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
				set count 0
				## At Least one Badword  to chek
				set bw "http:// www. ftp:/"
				set bw *[join [split $bw] "*\n*"]*
				set bwfound [lsearch -regexp -all -inline $arg {(http://|www.|ftp://)}]
				putquick "privmsg $chan : $bwfound"
				foreach bwb $bw {
				while {$count < [llength $bwfound]} {
					if { [string match -nocase "$bwb" [lindex $bwfound $count]] == 1 } {
						putquick "PRIVMSG $chan : Wypierdalam cie bo podałeś badword."
						#putquick "PRIVMSG $chan : $Excepts(list)"
						if { [string match -nocase "$Excepts(list)" [lindex $bwfound $count]] == 1 } {
							putquick "PRIVMSG $chan : Ale jest wyjatek to mash spokój bo spokoje. :p"
						} else {
							putquick "PRIVMSG $chan : Wyjątku tez brak to wypad."
							return 0
						}
					}
					incr count 1
				}
				}			
		}
			}
The problem is:
script work only when i type http:// ( it is added in badword list file ), after wchich is all ok.

When i typed "www." (it is added to badword list too and it is typeof 3 too) it doesn't do anything.

When i typed lolz www. it doesnt do anything only error -> 10.:141910·142210·141210:. 03«dghnws03» [19:22] Tcl error [badword:pubm:check]: can not find channel named "file33"

I want to it work for www. and ftp:/ and maybe others too not only for http://

this part is for check for except in a badword typed it working when i tested it with http:// but when i typed http://lol (lol is except added to except file) it has error : 10.:141910·142410·141210:. 03«dghnws03» [19:24] Tcl error [badword:pubm:check]: can not find channel named "file37"
The part is :

Code: Select all

					if { [string match -nocase "$bwb" [lindex $bwfound $count]] == 1 } {
						putquick "PRIVMSG $chan : Wypierdalam cie bo podałeś badword."
						#putquick "PRIVMSG $chan : $Excepts(list)"
						if { [string match -nocase "$Excepts(list)" [lindex $bwfound $count]] == 1 } {
							putquick "PRIVMSG $chan : Ale jest wyjatek to mash spokój bo spokoje. :p"
						} else {
							putquick "PRIVMSG $chan : Wyjątku tez brak to wypad."
							return 0
						}
					}
but it was working.

10.:141910·142410·145310:. 03«~Igor03» http://
10.:141910·142410·145410:. 03«@dghnws03» cos znalazlem! :D
10.:141910·142410·145410:. 03«@dghnws03» http://
10.:141910·142410·145410:. 03«@dghnws03» Wypierdalam cie bo podałeś badword.
10.:141910·142410·145410:. 03«@dghnws03» Wyjątku tez brak to wypad.
10.:141910·142510·140010:. 03«~Igor03» http://lol
10.:141910·142510·140110:. 03«@dghnws03» cos znalazlem! :D
10.:141910·142510·140110:. 03«@dghnws03» http://lol
10.:141910·142510·140110:. 03«@dghnws03» Wypierdalam cie bo podałeś badword.
10.:141910·142510·140110:. 03«@dghnws03» Ale jest wyjatek to mash spokój bo spokoje. :p
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

First off, why on earth do you use $bw as the list for foreach? You've joined back into a string!
As for your regular expression, "." is a special atom matching any single character.
Next, your "no such channel" error...
It's from your exceptions-file, or rather your incorrect reading of it:

Code: Select all

         while { ![eof $Excepts(File)] } { 
            gets $Excepts(File) line
            set Excepts(list) [string range $line 0 [expr [string first # $line]-1]]
            close $Excepts(File)
Looking at this piece of the code makes it obvious that you open the file, starts a while-loop checking for an eof-conditon on the opened file. You then read it once, and directly afterwards, you close it... so when the body fo the while-loop reaches the end and the conditional of the while-loop is tested, $Excepts(File) has already been closed, and thus the file-descriptor (or channel) is no longer valid...

Finally, I'm curious as to the source for $arg, which you use in your lsearch.
NML_375
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

Code: Select all

				while {$count < [llength $bwfound]} {
					if { [string match -nocase "$bw" [lindex $bwfound $count]] == 1 } {
						putquick "PRIVMSG $chan : Wypierdalam cie bo podałeś badword."
						#putquick "PRIVMSG $chan : $Excepts(list)"
						if { [string match -nocase "$Excepts(list)" [lindex $bwfound $count]] == 1 } {
							putquick "PRIVMSG $chan : Ale jest wyjatek to mash spokój bo spokoje. :p"
						} else {
							putquick "PRIVMSG $chan : Wyjątku tez brak to wypad."
							return 0
						}
					}
					incr count 1
				}
now it dont want work anyhow ..

it is possible to change that . to ? ?
coz i really need chars like . to be normal -,-

--
lsearch is working with $ARG :p
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I fail to see whatever you've changed in the posted piece of code from the original post.

In order to use . with regular expressions you'll have to escape them properly. I'd strongly suggest you study the basics on regular expressions, as these can quite quickly become very complex and not behave as intended.

Yes, I know you use lsearch to scan through $arg. That was not my question. The question was where the value of arg is defined/set.

Oh, btw, just saying "it does'nt work" and not providing any more details really does'nt help...
NML_375
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

Only this part does not work which i paste you higher . And as i think, script dont even read this part. If i use bwb $bw and then when i change $bw on $bwb in a place "if string match -nocase" this $bwb works. Problem with this comunicate - ubm:check]: can not find channel named "file" - gone after i delete - close excepts-file -.

However i still dont know what i have to do with this "." as u wrote to me. I think it should be recognize as a simple sign, and i cant use / before "." because i have to write !badword add *www* before. then in my file it should to be wrote as www. I dont have any problems with this command but as u told me ..laters eggdrop will read this as any sign, it should to exist something like "simple full stop" (".")
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

You'll have to escape the . if you want it to match period (.) only... There's no other way of doing it with regular expressions.
As for bw and bwb, in the last code example including that part that you've posted, bw is a string, and as such is unsuitable to be used with foreach (as I stated in a previous post aswell).

If you believe the part of the code you pasted isn't even executed, why do you think the error is there in the first place?
NML_375
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

There is no any errors,

i said http:// wchich is added to $bw and it didnt do anything,

it should say at least putquick "PRIVMSG $chan : Wypierdalam cie bo podałeś badword."
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

$bw dont want work as list in "if string match - nocase" always returnining 0 -.-
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

So you've removed the foreach loop then?
NML_375
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

i said you i did - if not sorry then

but it also dont working with foreach .. just maybe with first badword "http://" it dont see "www." and others when foreach
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

So, if you don't use foreach anymore, why should bw match "http://", assuming it's still set according to this:

Code: Select all

set bw "http:// www. ftp:/"
set bw *[join [split $bw] "*\n*"]*
Effectively setting bw to this:
*http://*
*www.*
*ftp:/*
I doubt any of the list-items within $bwfound would match that pattern, however I would need further information on $arg to effectively say wether it would actually have a chance of matching anything...
NML_375
C
Craig
Voice
Posts: 18
Joined: Wed Feb 22, 2006 2:18 pm

Post by Craig »

Code: Select all

bind pubm - "* *" badword:pubm:check 
proc badword:pubm:check {nick uhost handle chan arg} { 
	global triggerchar
	## if { [isop $nick $chan] || [ishalfop $nick $chan] || [isvoice $nick $chan] } { 
	##	putlog "used isop/hop/vop"
	##	return 0 
	## } 
	if {[string match "*@..." $uhost]} { 
		 putlog "is hosted"
		 return 0 
	}
	if { ![file exists [string trimleft $chan #]_badwords.txt] } {
		return 0
	}
	set input [swear:filter2 $arg]
	set readbw [open [string trimleft $chan #]_badwords.txt r] 
	while { ![eof $readbw] } {  
	putlog "i do something"
	gets $readbw line 
	set helpwanted [string range $line 0 [expr [string first # $line]-1]] 
	set message [string range $line [expr [string first # $line]+1] [expr [string first ź $line]-1]] 
	set typeof  [string range $line [expr [string first ź $line]+1] end] 
if { ([string match -nocase "* $helpwanted *" $input] == 1 && $helpwanted != "") || ([string match -nocase "$helpwanted" $input] == 1 && $helpwanted != "") } {
                if { $typeof == 4 } {
                        BadWordKick $nick $uhost $chan $arg $helpwanted $message $typeof $input
                        return 0
                }
		if { $typeof == 3 } {
			putquick "PRivmsg $chan : cos znalazlem! :D"
			set Excepts(File) [open excepts_[string trimleft $chan #]_badwords.txt r] 
			while { ![eof $Excepts(File)] } {  
				gets $Excepts(File) line
				set Excepts(list) [string range $line 0 [expr [string first # $line]-1]]
				## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
				set count 0
				## At Least one Badword  to chek
				set bw "http:// www. ftp:/"
				set bw *[join [split $bw] "*\n*"]*
				set bwfound [lsearch -regexp -all -inline $arg {(http://|www.|ftp://)}]
				putquick "privmsg $chan : $bwfound"
				putquick "privmsg $chan : $bw"
				putquick "privmsg $chan : $Excepts(list)"
				

				while {$count < [llength $bwfound]} {
					if { [string match -nocase "$bw" [lindex $bwfound $count]] == 1 } {
						putquick "PRIVMSG $chan : Wypierdalam cie bo podałeś badword."
						#putquick "PRIVMSG $chan : $Excepts(list)"
						if { [string match -nocase "$Excepts(list)" [lindex $bwfound $count]] == 1 } {
							putquick "PRIVMSG $chan : Ale jest wyjatek to mash spokój bo spokoje. :p"
						} else {
							putquick "PRIVMSG $chan : Wyjątku tez brak to wypad."
							return 0
						}
					}
					incr count 1
				}
		
		}
			}
etc., so $arg return what someone said ... ;p of course i tried to say http:// and ftp://
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

First off then, you cannot use lsearch on $arg, as it is not a list...
Secondly, since arg is provided from the pubm-trigger, it cannot contain a newline; hence the pattern you store in bw cannot match any possible result from your lsearch (as should be obvious from my last post)
That is, since arg never could hold any newlines, and you type http://, at best, the lsearch would result in bwfound being set to:
http://
Quite obviously, bw cannot match this pattern:
*http://*
*www.*
*ftp:/*
NML_375
Post Reply