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]"