| View previous topic :: View next topic |
| Author |
Message |
Red_Rooste5 Voice
Joined: 21 Oct 2006 Posts: 37
|
Posted: Sat Dec 02, 2006 4:13 pm Post subject: regexp help. |
|
|
| Code: | proc anagram:public {nick uhost hand chan text} {
set anagram [string map {" " "%20"} $text]
set site "http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=$anagram"
set token [::http::geturl $site]
set content [::http::data $token]
regexp {(.+?) ^ (.+?)<br>} $content match name answer
putquick "PRIVMSG $chan :\00302Anagram lookup for: \00307$text"
putquick "PRIVMSG $chan :\00307$name \00302- \00307$answer"
}
|
Well, I get the answer:
[21:12] Tcl error [anagram:public]: can't read "name": no such variable
If you want to look at the parser source, use a bas instead of $anagram
I really need help with this script. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Dec 02, 2006 4:53 pm Post subject: |
|
|
I'd suggest you check wether your regexp matched the epxression before trying to use the submatch variables.
In this case, as the webpage returns "No match for anagram<br><br>", it won't match that regexp (and thus no submatches either).
Try something like this:
| Code: | proc anagram:public {nick uhost hand chan text} {
set anagram [string map {" " "%20"} $text]
set site "http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=$anagram"
set token [::http::geturl $site]
set content [::http::data $token]
if {[regexp {(.+?) ^ (.+?)<br>} $content match name answer]} {
putquick "PRIVMSG $chan :\00302Anagram lookup for: \00307$text"
putquick "PRIVMSG $chan :\00307$name \00302- \00307$answer"
}
} |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Sat Dec 02, 2006 6:38 pm Post subject: |
|
|
I'd also init the vars with set name "" and set answer "" then you can test if $name != "" and so forth.
And generally, you want to test that the geturl operation was successful:
| Code: |
#antiwordwrap#################################################################################
set url "http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=$anagram"
catch {set page [::http::geturl $url -timeout 10000]} error
if {[string match -nocase "*couldn't open socket*" $error]} {
puthelp "PRIVMSG $nick :Error: couldn't connect..Try again later"
return
}
if { [::http::status $page] == "timeout" } {
puthelp "PRIVMSG $nick :Error: Connection timed out."
return
}
set html [::http::data $page]
::http::cleanup $page
|
That's the basic gist of it. Don't forget to call ::http::cleanup when you're done pulling data in, so you're not leaving open sockets behind. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Dec 03, 2006 4:54 am Post subject: |
|
|
Actually, you'd have better options if you do not init those vars to "".
Namley "info exists <variable>" _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Sun Dec 03, 2006 6:36 am Post subject: |
|
|
| I keep forgetting about the [info exists] cmd, I need to get in the habit of using it (too many years of shell-scripting..) |
|
| Back to top |
|
 |
Red_Rooste5 Voice
Joined: 21 Oct 2006 Posts: 37
|
Posted: Sun Dec 03, 2006 7:36 am Post subject: |
|
|
| nml375 wrote: | I'd suggest you check wether your regexp matched the epxression before trying to use the submatch variables.
In this case, as the webpage returns "No match for anagram<br><br>", it won't match that regexp (and thus no submatches either).
Try something like this:
| Code: | proc anagram:public {nick uhost hand chan text} {
set anagram [string map {" " "%20"} $text]
set site "http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=$anagram"
set token [::http::geturl $site]
set content [::http::data $token]
if {[regexp {(.+?) ^ (.+?)<br>} $content match name answer]} {
putquick "PRIVMSG $chan :\00302Anagram lookup for: \00307$text"
putquick "PRIVMSG $chan :\00307$name \00302- \00307$answer"
}
} |
|
I used that but now it msgs nothing and I'm not getting any error msgs. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Dec 03, 2006 8:01 am Post subject: |
|
|
It's because the webpage returns "No match for anagram<br><br>". If you would post the output of a proper query, we could verify if your regexp matches the format used or not, and possibly fix it. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sun Dec 03, 2006 9:37 am Post subject: |
|
|
| Code: |
if {[catch {package require http} err]} return
proc anagram:public {nick uhost hand chan text} {
if {![llength [set anagram [lindex [split $text] 0]]]} {
putserv "PRIVMSG $chan :Specify an anagram"
return
}
set token [::http::geturl "http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=$anagram"]
set content [::http::data $token]
::http::cleanup $content
regsub -all {<[^>]+>|<} $content { } content
if {[regexp {(.+?) ^ (.+?)<br>} $content match name answer]} {
putquick "PRIVMSG $chan :\00302Anagram lookup for: \00307$text"
putquick "PRIVMSG $chan :\00307$name \00302- \00307$answer"
}
# debug?
# putserv "PRIVMSG $chan :$content"
}
|
Tell me something valid to test in case it dosen't seem to be working. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
Red_Rooste5 Voice
Joined: 21 Oct 2006 Posts: 37
|
Posted: Sun Dec 03, 2006 1:26 pm Post subject: |
|
|
Well, I made EXACTLY your script, though I get the msg:
| Quote: | can't read "content": no such variable
while executing
"proc anagram:public {nick uhost hand chan text} {
if {![llength [set anagram [lindex [split $text] 0]]]} {
putquick "PRIVMSG $chan :Specify an anagram..."
(file "scripts/anagram.tcl" line 7)
invoked from within
"source scripts/anagram.tcl"
(file "eggdrop.conf" line 1343)
|
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Dec 03, 2006 2:49 pm Post subject: |
|
|
As long as the webpage returns "No match for anagram<br><br>", none of the scripts posted here will "work". And frankly, I'm not sure how to mine the data out of that (what would be "name" and what would be "answer" in that text), I'd rather guess this output would be some error message that the anagram was not found.
So, once again, please provide us with a valid result from that php-script so we know what to look for... _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Red_Rooste5 Voice
Joined: 21 Oct 2006 Posts: 37
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Mon Dec 04, 2006 12:19 pm Post subject: |
|
|
Space got lost in your link, but adding %20 instead of the space worked just well.
The problem with your regular expression is that ^ is interpreted as a special atom, namely beginning of line. You'll have to escape it within your regular expression to be able to match a ^-char explicitly.
The following should work well for you (using rosc's suggestions on implementation of the http-package):
| Code: | proc anagram:public {nick uhost hand chan text} {
set anagram [string map {" " "%20"} $text]
set url "http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=$anagram"
catch {set page [::http::geturl $url -timeout 10000]} error
if {[string match -nocase "*couldn't open socket*" $error]} {
puthelp "PRIVMSG $nick :Error: couldn't connect..Try again later"
return
}
if { [::http::status $page] == "timeout" } {
puthelp "PRIVMSG $nick :Error: Connection timed out."
return
}
set content [::http::data $page]
::http::cleanup $page
if {[regexp {(.+?) \^ (.+?)<br>} $content match name answer]} {
puthelp "PRIVMSG $chan :\00302Anagram lookup for: \00307$text"
puthelp "PRIVMSG $chan :\00307$name \00302- \00307$answer"
} {
puthelp "PRIVMSG $chan :Your query for $text did not yield any results"
}
} |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|