egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

regexp help.

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Red_Rooste5
Voice


Joined: 21 Oct 2006
Posts: 37

PostPosted: Sat Dec 02, 2006 4:13 pm    Post subject: regexp help. Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Dec 02, 2006 4:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Sat Dec 02, 2006 6:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Dec 03, 2006 4:54 am    Post subject: Reply with quote

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
View user's profile Send private message
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Sun Dec 03, 2006 6:36 am    Post subject: Reply with quote

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
View user's profile Send private message
Red_Rooste5
Voice


Joined: 21 Oct 2006
Posts: 37

PostPosted: Sun Dec 03, 2006 7:36 am    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Dec 03, 2006 8:01 am    Post subject: Reply with quote

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
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Sun Dec 03, 2006 9:37 am    Post subject: Reply with quote

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
View user's profile Send private message
Red_Rooste5
Voice


Joined: 21 Oct 2006
Posts: 37

PostPosted: Sun Dec 03, 2006 1:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Dec 03, 2006 2:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
Red_Rooste5
Voice


Joined: 21 Oct 2006
Posts: 37

PostPosted: Mon Dec 04, 2006 11:47 am    Post subject: Reply with quote

http://www.slushpuppy.silverinterlocution.org/anagrams.php?anagrams=a bas
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Mon Dec 04, 2006 12:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber