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.

reading a specific line from a textfile

Old posts that have not been replied to for several years.
Locked
m
mrQ_

Post by mrQ_ »

Hello,

I'm very new at tcl scripting, sure I made some simple !op !topic scripts but now I want to try something more advanced.

I'd like to be able to use a command like
!search stringtofind and then have the script search through a textfile for a line beginning with stringtofind and then msg it to the person who gave the command.

How do I go about this ?

thx!
Quinten
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

set fs [open $file r]
  while {![eof $fs]} { 
    gets $fs line 
    if {[string match "stringtofind*" $line]} {
      putserv "PRIVMSG $nick :$line" 
    }
  }
try that one...
_________________
Elen sila lúmenn' omentielvo

<font size=-1>[ This Message was edited by: Papillon on 2002-06-05 11:29 ]</font>
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

Personally I wouldn't dump the text to server and continue reading the file like that. I'd put each matching line into a list, and then evaluate how long that list is before dumping each individual line to the server. This way, if like 1000 lines match, its possible, for example, to dump out only the first 5 lines (or maybe 5 random lines).
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Papillon - it's good, but you should check for eof after gets not before (although in this case it doesn't really matter). Also, the code is generally faster if you use read/split instead of a loop.

Code: Select all

set fp [open $file r]
set lines [split [read $fp n]
close $fp
set i [lsearch -glob $lines "$sometext*"]
if {$i != -1} {
  putserv "privmsg $nick :[lindex $lines $i]
} else {
  putserv "privmsg $nick :not found!"
}
Locked