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.

Search txt file for matching text and display

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Search txt file for matching text and display

Post by BigToe »

Hi, I`m looking for a script to work with the following format of a text file:

[Category1]
line1
line2
line3
[Category2]
line1
line2
line3

When a user types !categories

The bot will display all the existing [Category] that exists in a text file (the text file name is "scripts/Categories.txt") to the user


After displaying the categories, or even before (if a user knows what the categories are without asking for displaying them), the user can pick a category via !search Category1 and the bot will display to him all the lines in that category.

Example:

Categories.txt format is as following:

[Fruits]
Apple
Banana
Orange
[Movies]
Matrix
Kill Bill
Fast and Furious
[Music]
Oasis
Metallica
Aerosmith

<User> !categories
<Bot> Fruits, Movies, Music.

<User> !Category Fruits
<Bot> Apple
<Bot> Banana
<Bot> Orange
<User> !Category Music
<Bot> Oasis
<Bot> Metallica
<Bot> Aerosmith
* In addition, a !randCategory function that will display 1 random line from 1 random category, would be brilliant as-well.
Please make sure the interval of displaying is such that will not force him to quit due to flood.

Thanks!
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Try this and see what we have so far:)

Code: Select all

# set the route and file name of the categories file #
set cats(file) "scripts/Categories.txt"

# set the command to list all the categories #
set cats(ccmd) "!categories"

# set the command to read one of the categories #
set cats(rcmd) "!category"

# set the user file flags required to use this script ("-" = everyone) #
set cats(flag) "-"


###### END OF SETTINGS ######


set cats(cats) ""

if {![file exists $cats(file)]} {  return  }

set cats(tm-open) [open $cats(file)]
while {![eof $cats(tm-open)]} {
 set cats(tm-line) [gets $cats(tm-open)]
 if {[string match \[*\] $cats(tm-line)]} {
   lappend cats(cats) [string range $cats(tm-line) 1 end-1]
 }
}
close $cats(tm-open)

if {$cats(cats) eq ""} {  return  }


bind pub $cats(flag) $cats(ccmd) cats:listcats 

proc cats:listcats {nk uh hn ch tx} { 
 if {$cats(cats) eq ""} {  return  }
 puthelp "PRIVMSG $ch :[join $cats(cats) ", "]."
}


bind pub $cats(flag) $cats(rcmd) cats:readacat 

proc cats:readacat {nk uh hn ch tx} { 
 if {$cats(cats) eq ""} {  return  }
 set tx [string trim $tx]

 if {[lsearch -exact $cats(cats) $tx]>"-1"} {  set patrn \[$tx\]
 } elseif {[lsearch $cats(cats) $tx*]>"-1"} {  set patrn \[$tx*\]
 } elseif {[lsearch $cats(cats) *$tx*]>"-1"} {  set patrn \[*$tx*\]
 } else {  return  }

 set open [open $cats(file)]  ;  set found 0
 while {![eof $open]} {
  set line [gets $open]
  if {$line eq ""} {  continue  }
  if {[string match $patrn $line]} {  set found 1
  } elseif {[string match \[*\] $line]} {
    if {$found>"0"} {  break  }
  } elseif {$found>"0"} {  
    puthelp "PRIVMSG $ch :$line"
  }
 }
 close $open
}

putlog "cats.tcl v1.1 loaded."

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Post by BigToe »

Hey SpiKe^^,

Thanks for the reply.

I've loaded a script and created Categories.txt in the following format:

[Fruits]
Banana
Apple
[Old Movies]
Kill Bill
Matrix

Tried to trigger it via !Categories, it did not work. Also tried !category Fruits and that didn't work either
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Cats, take 2

Post by SpiKe^^ »

You're right, that code won't run.
Had a few string match issues:)
This code should run without error.

Code: Select all

# set the route and file name of the categories file #
set cats(file) "scripts/Categories.txt"

# set the command to list all the categories #
set cats(ccmd) "!categories"

# set the command to read one of the categories #
set cats(rcmd) "!category"

# set the user file flags required to use this script ("-" = everyone) #
set cats(flag) "-"


###### END OF SETTINGS ######


set cats(cats) ""
if {![file exists $cats(file)]} {  return  }

set cats(tm-open) [open $cats(file)]
while {![eof $cats(tm-open)]} {
 set cats(tm-line) [string trim [gets $cats(tm-open)]]
 if {[string match {\[*\]} $cats(tm-line)]} {
   lappend cats(cats) [string range $cats(tm-line) 1 end-1]
 }
}
close $cats(tm-open)

if {$cats(cats) eq ""} {  return  }


bind pub $cats(flag) $cats(ccmd) cats:listcats

proc cats:listcats {nk uh hn ch tx} {  global cats
 if {$cats(cats) eq ""} {  return  }
 puthelp "PRIVMSG $ch :[join $cats(cats) ", "]."
}


bind pub $cats(flag) $cats(rcmd) cats:readacat

proc cats:readacat {nk uh hn ch tx} {  global cats
 if {$cats(cats) eq ""} {  return  }
 set tx [string trim [string tolower $tx]]

 if {$tx eq ""} {
   puthelp "PRIVMSG $ch :${nk}: Use:  $cats(rcmd) <category-name>"
   return
 }

 set pexact 0
 if {[lsearch -exact [string tolower $cats(cats)] $tx]>"-1"} {  set patrn $tx  ;  set pexact 1
 } elseif {[lsearch [string tolower $cats(cats)] $tx*]>"-1"} {  set patrn $tx*
 } elseif {[lsearch [string tolower $cats(cats)] *$tx*]>"-1"} {  set patrn *$tx*
 } else {
   puthelp "PRIVMSG $ch :${nk}: No matching category found."
   return
 }

 set open [open $cats(file)]  ;  set found 0
 while {![eof $open]} {
  set line [string trim [gets $open]]
  if {$line eq ""} {  continue  }

  set x ""
  if {[string match {\[*\]} $line]} {  set x [string range $line 1 end-1]  }

  if {$x ne "" && [string match -nocase $patrn $x]} {  set found 1  ;  set z $x
  } elseif {$x ne ""} {
    if {$found>"0"} {  break  }
  } elseif {$found>"0"} {

    if {$found=="1"} {  set found 2
      if {$pexact=="0"} {  puthelp "PRIVMSG $ch :Reading category:  $z"  }
    }

    puthelp "PRIVMSG $ch :$line"
  }
 }
 close $open
}

putlog "cats.tcl v1.1 loaded."

Last edited by SpiKe^^ on Fri Aug 31, 2012 6:08 pm, edited 1 time in total.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
B
BigToe
Halfop
Posts: 99
Joined: Thu Dec 30, 2010 4:49 pm

Post by BigToe »

Perfect Spike^^!

Great job, appreciate it!
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Edited the above code some...
You should get the code again.
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Post Reply