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 

Search txt file for matching text and display

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
BigToe
Halfop


Joined: 30 Dec 2010
Posts: 99

PostPosted: Thu Aug 30, 2012 10:39 am    Post subject: Search txt file for matching text and display Reply with quote

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


Quote:

<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!
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Thu Aug 30, 2012 10:36 pm    Post subject: Reply with quote

Try this and see what we have so far:)

Code:

# 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
.
Back to top
View user's profile Send private message Visit poster's website
BigToe
Halfop


Joined: 30 Dec 2010
Posts: 99

PostPosted: Fri Aug 31, 2012 2:24 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Fri Aug 31, 2012 5:00 pm    Post subject: Cats, take 2 Reply with quote

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

Code:

# 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."


_________________
SpiKe^^

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


Last edited by SpiKe^^ on Fri Aug 31, 2012 6:08 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
BigToe
Halfop


Joined: 30 Dec 2010
Posts: 99

PostPosted: Fri Aug 31, 2012 5:28 pm    Post subject: Reply with quote

Perfect Spike^^!

Great job, appreciate it!
Back to top
View user's profile Send private message
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Fri Aug 31, 2012 6:09 pm    Post subject: Reply with quote

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
.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests 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