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.

adding a search

Help for those learning Tcl or writing their own scripts.
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

adding a search

Post by streamish »

My script is as follows:

Code: Select all

bind pub - !shoelace pub_shoelace

proc pub_shoelace {nick mask hand channel args} {
   global shoelace
     putserv "PRIVMSG $channel :\[RANDOM IMAGE\] [lindex $shoelace [rand [llength $shoelace]]]"
      }

      set shoelace {

"1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg"
"2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg"
"3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg"
etc etc etc etc }
Basically all it does is randomly pulls a link from 'shoelace'. and it works wonderfully. My question is, how can I possibly add a function to where, if I execute '!shoelace keyword' it tries to match the keyword to a link in 'shoelace' and putserv the relevant link, if not, else if to a random link.

Any input would be great.
Thanks!
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

You could use the lsearch command to search through your list for a given pattern.

PS. I took the liberty of moving this thread to the "Scripting Help" forum. DS
NML_375
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

I added this line of code to search for the argument

Code: Select all

putserv "PRIVMSG $channel :\[LOCATED IMAGE\] [lindex $shoelace [rand [lsearch -all $shoelace $args]]]"
But it is returning this error...


Code: Select all

[15:30:35] Tcl error [pub_shoelace]: random limit must be greater than zero
I'm fairly new to TCL, so again, any guidance is appreciated.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

First off, I doubt you'd like to use rand with the search-result; didn't you search for one specific item within the list? If you were thinking of picking a random item from the search result list (since you used -all), that won't work at all; "rand" expects a single integer, and returns a value between 0 and the provided integer minus one.

Secondly, using "args" as the last argument name will cause issues with this code. Use something different such as "text" or "data", which is not handled in a special manner by tcl.

I'd first check if the user provided any text:

Code: Select all

if {$text != ""} {
#user supplied a search-term
  set hits [lsearch -all -- $shoelace $text]

  #test if we found any..
  if {[llength $hits] > 0} {
    #We've got atleast one match, pick a random one if we've got multiple ones
    set item [rand [llength $hits]]

  } else {
    #No hits, pick a random line from the list
    set item [rand [llength $shoelace]]
  }
} else {

#No search-term supplied, pick a random item
  set item [rand [llength $shoelace]]
}
Now that I've got the index of either a random result, use it to extract the line from the list and print it..

Code: Select all

puthelp "PRIVMSG $channel :[lindex $shoelace $item]"
NML_375
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

Thanks nml375, I love how you set that up. It works beautifully. The only problem I'm seeing now is the matching.

Code: Select all

 set shoelace {

"1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.j$
"2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg"
"3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg"
"4 of 3918 http://shoelace.org/pics/2011/03-02-2011/6e3733137623ec36878d18dd4c8993ed.jpg"
"5 of 3918 http://shoelace.org/pics/2011/03-02-2011/523px-Michael_Pacher_004.jpg"
"6 of 3918 http://shoelace.org/pics/2011/03-02-2011/tattoo.jpg"
"7 of 3918 http://shoelace.org/pics/2011/03-02-2011/310765_ShayMaria.jpg"
"8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg"
"9 of 3918 http://shoelace.org/pics/2011/03-02-2011/McDuffie.jpg"
"10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg"
"11 of 3918 http://shoelace.org/pics/2011/03-02-2011/day.jpg"
"12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg"
"13 of 3918 http://shoelace.org/pics/2011/03-02-2011/1256244960-1245496056908.jpg"
Attempting to fetch the image link for "far.jpg". It's pulling random image links

Code: Select all

<@stream> !test far
<oldskewl> 3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg
<@stream> !test far.jpg
<oldskewl> 10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg
<@stream> !test far.jpg
<oldskewl> 10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg
<@stream> !test far.jpg
<oldskewl> 12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg
<@stream> !test far.jpg
<oldskewl> 16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

I'm guessing the issue lies within the following:

Code: Select all

  #test if we found any..
  if {[llength $hits] > 0} {
    #We've got atleast one match, pick a random one if we've got multiple ones
    set item [rand [llength $hits]]

  } else {
    #No hits, pick a random line from the list
    set item [rand [llength $shoelace]]
  }
} else {

#No search-term supplied, pick a random item
  set item [rand [llength $shoelace]]
}

putserv "PRIVMSG $channel :[lindex $shoelace $item]"
}
If it's finding the match, it's not printing it to channel, rather it continues down the code to

Code: Select all

putserv "PRIVMSG $channel :[lindex $shoelace $item]"
}
In which it's just pulling a random link due to

Code: Select all

} else {
    #No hits, pick a random line from the list
    set item [rand [llength $shoelace]]
But I am definitely no expert.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

set hits [lsearch -all -- $shoelace $text] 
The code nml375 gave does work just fine. In using only the -all switch, it will be assumed a "glob" match. Problem is, there are no wildcards (* or ?) to facilitate a proper glob match. Meaning to get nml375's code to work right, you need to issue your request like this, including wildcards in your text:

!shoelace *far*


To get around this limitation, and have the script assume wildcards * begin and end the string. Change the code above to look like it is below. Once you do this, the following will work like it does above, without having to use *'s within your text.

!shoelace far

Code: Select all

set hits [lsearch -all -- $shoelace *$text*] 
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

Didn't seem to work for me.

My code full code is as follows:

Code: Select all

bind pub - !test pub_shoelace

proc pub_shoelace {nick mask hand channel text} {
global shoelace

if {$text != ""} {
#user supplied a search-term
  set hits [lsearch -all $shoelace *$text*]

  #test if we found any..
  if {[llength $hits] > 0} {
    #We've got atleast one match, pick a random one if we've got multiple ones
    set item [rand [llength $hits]]

  } else {
    #No hits, pick a random line from the list
    set item [rand [llength $shoelace]]
  }
} else {

#No search-term supplied, pick a random item
  set item [rand [llength $shoelace]]
}

putserv "PRIVMSG $channel :[lindex $shoelace $item]"
}


set shoelace {

"1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.j$
"2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg"
"3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg"
"4 of 3918 http://shoelace.org/pics/2011/03-02-2011/6e3733137623ec36878d18dd4c8993ed.jpg"
"5 of 3918 http://shoelace.org/pics/2011/03-02-2011/523px-Michael_Pacher_004.jpg"
"6 of 3918 http://shoelace.org/pics/2011/03-02-2011/tattoo.jpg"
"7 of 3918 http://shoelace.org/pics/2011/03-02-2011/310765_ShayMaria.jpg"
"8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg"
"9 of 3918 http://shoelace.org/pics/2011/03-02-2011/McDuffie.jpg"
"10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg"
"11 of 3918 http://shoelace.org/pics/2011/03-02-2011/day.jpg"
"12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg"
"13 of 3918 http://shoelace.org/pics/2011/03-02-2011/1256244960-1245496056908.jpg"
"14 of 3918 http://shoelace.org/pics/2011/03-02-2011/AVvFQ.jpg"
"15 of 3918 http://shoelace.org/pics/2011/03-02-2011/Blizzy.jpg"
"16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg"
"17 of 3918 http://shoelace.org/pics/2011/03-02-2011/DSC05480.jpg"
"18 of 3918 http://shoelace.org/pics/2011/03-02-2011/F1EjB.jpg"
"19 of 3918 http://shoelace.org/pics/2011/03-02-2011/HRguidetoconversation.jpg"

"20 of 3918 http://shoelace.org/pics/2011/03-02-2011/Lol_wut_completely.jpg"
"21 of 3918 http://shoelace.org/pics/2011/03-02-2011/MJ3eN.jpg"
"22 of 3918 http://shoelace.org/pics/2011/03-02-2011/Meanwhile_In_Uganda.jpg"

}
However, I get the following return results:

Code: Select all

<@stream> !test day
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
<@stream> !test *day*
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
<@stream> !test
<oldskewl> 16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg
<@stream> !test day
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
<@stream> !test McDuffie
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

I guess I spoke too soon. HAW

nml375's code has one major flaw within it.

Code: Select all

    #We've got atleast one match, pick a random one if we've got multiple ones
    set item [rand [llength $hits]] 
The part here that sets item is to blame. This checks length of "hits" which will be the amount of positions found that hold your results. It then picks a random number from 0 to length. This is where it fails. It should apply that to a lindex to get the position from $hits which would then work correctly.

Code: Select all

bind pub - !shoelace pub_shoelace

proc pub_shoelace {nick mask hand channel text} {
  global shoelace

  if {[string length [string trim $text]]} {
    #user supplied a search-term
    set hits [lsearch -all $shoelace *[string trim $text]*]

    #test if we found any..
    if {[llength $hits] > 0} {
      #We've got atleast one match, pick a random one if we've got multiple ones
      set item [lindex $hits [rand [llength $hits]]] ; set status "SEARCH FOUND"
    } else {
      #No hits, pick a random line from the list
      set item [rand [llength $shoelace]] ; set status "RANDOMIZED"
    }
  } else {
    #No search-term supplied, pick a random item
    set item [rand [llength $shoelace]] ; set status "RANDOMIZED"
  }
  putserv "PRIVMSG $channel :\[$status\] [lindex $shoelace $item]"
}

set shoelace {
  {1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg}
  {2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg}
  {3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg}
  {4 of 3918 http://shoelace.org/pics/2011/03-02-2011/6e3733137623ec36878d18dd4c8993ed.jpg}
  {5 of 3918 http://shoelace.org/pics/2011/03-02-2011/523px-Michael_Pacher_004.jpg}
  {6 of 3918 http://shoelace.org/pics/2011/03-02-2011/tattoo.jpg}
  {7 of 3918 http://shoelace.org/pics/2011/03-02-2011/310765_ShayMaria.jpg}
  {8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg}
  {9 of 3918 http://shoelace.org/pics/2011/03-02-2011/McDuffie.jpg}
  {10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg}
  {11 of 3918 http://shoelace.org/pics/2011/03-02-2011/day.jpg}
  {12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg}
  {13 of 3918 http://shoelace.org/pics/2011/03-02-2011/1256244960-1245496056908.jpg}
  {14 of 3918 http://shoelace.org/pics/2011/03-02-2011/AVvFQ.jpg}
  {15 of 3918 http://shoelace.org/pics/2011/03-02-2011/Blizzy.jpg}
  {16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg}
  {17 of 3918 http://shoelace.org/pics/2011/03-02-2011/DSC05480.jpg}
  {18 of 3918 http://shoelace.org/pics/2011/03-02-2011/F1EjB.jpg}
  {19 of 3918 http://shoelace.org/pics/2011/03-02-2011/HRguidetoconversation.jpg}
  {20 of 3918 http://shoelace.org/pics/2011/03-02-2011/Lol_wut_completely.jpg}
  {21 of 3918 http://shoelace.org/pics/2011/03-02-2011/MJ3eN.jpg}
  {22 of 3918 http://shoelace.org/pics/2011/03-02-2011/Meanwhile_In_Uganda.jpg}
}
I've tested this, it works perfectly. Also use { } to enclose your quasi-list of shoelace url's. This will avoid many problems that arise from hand-crafted lists.
<speechles> !shoelace far
<sp33chy> [SEARCH FOUND] 8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

Thanks so much speechles, it's absolutely brilliant.
What would the difficulty be to seperate the links into an external file such as links.txt?

Would I just do something like this

Code: Select all

set file "scripts/links.txt"

set shoelace [open $file r]
Thanks
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Where the list presently is...

Code: Select all

set shoelace {
{etc}
{etc}
}
Change that entire section that sets shoelace to look exactly like the code below:

Code: Select all

set shoefile [open "scripts/links.txt" r]
set shoelace [split [read -nonewline $shoefile] "\n"]
close $shoefile ; unset shoefile
It will now read the shoelace list from your file. It isn't very difficult to do at all. ;)
Last edited by speechles on Fri Mar 25, 2011 12:05 am, edited 1 time in total.
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

Thanks man. You're the bestestestest. I'm trying to get adjusted to it, because tcl seems pretty damn fun.
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

Heres a question, would I be able to have a global database by changing the links.txt location to http://www.whateverdomain.com/links.txt?
s
streamish
Voice
Posts: 21
Joined: Fri Dec 03, 2010 4:33 pm

Post by streamish »

I actually haven't even tried yet, just figured I would ask lol
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

It wouldn't quite work that way. File handlers are not the same as http socket handlers.
A very basic way to accomplish it would be as follows:

Code: Select all

set shoefile [open "scripts/links.txt" r]
set shoelace [split [read -nonewline $shoefile] "\n"]
close $shoefile ; unset shoefile
Change what you see above, to look like it does below.

Code: Select all

package require http
if [catch {set shoefile [::http::geturl "http://www.whateverdomain.com/links.txt" -timeout 5000]} error]} {
  putlog "ERROR: Cannot create link list ( $error )"
  set shoelace [list] ; unset shoefile
} else {
  set shoelace [split [http::data $shoefile] "\n"]
  ::http::cleanup $shoefile ; unset shoefile
}
This of course doesn't account for redirects, cookies, secure sockets, gzip, other types of http transaction errors, etc. This method simply assumes your http call will succeed with a code 200 always. We will assume it always does. :)
Post Reply