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.

regexp help.

Help for those learning Tcl or writing their own scripts.
Post Reply
R
Red_Rooste5
Voice
Posts: 37
Joined: Sat Oct 21, 2006 7:43 am

regexp help.

Post by Red_Rooste5 »

Code: Select all

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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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: Select all

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
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

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: Select all

#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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Actually, you'd have better options if you do not init those vars to "".
Namley "info exists <variable>"
NML_375
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

I keep forgetting about the [info exists] cmd, I need to get in the habit of using it (too many years of shell-scripting..)
R
Red_Rooste5
Voice
Posts: 37
Joined: Sat Oct 21, 2006 7:43 am

Post by Red_Rooste5 »

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: Select all

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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Code: Select all

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.
R
Red_Rooste5
Voice
Posts: 37
Joined: Sat Oct 21, 2006 7:43 am

Post by Red_Rooste5 »

Well, I made EXACTLY your script, though I get the msg:
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)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
R
Red_Rooste5
Voice
Posts: 37
Joined: Sat Oct 21, 2006 7:43 am

Post by Red_Rooste5 »

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

Post by nml375 »

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: Select all

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
Post Reply