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.

readfile problem

Old posts that have not been replied to for several years.
Locked
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

readfile problem

Post by NewzUK »

Hi - I'm working on a script to read a text file but don't seem to be able to get past this...I'm not getting any errors:

bind pub - .cbsnews pub:readmkw
proc pub:readmkw { nick uhost handle channel arg } {
set mkwf [open /home/citinews/Bots/Undernet/news/mkw.txt r]
set mkw [lsearch -glob $mkwf "/04"]
if {$mkw != -1} {
putserv "PRIVMSG #Newsroom2 :$mkw"
} else {
return
}
}

The line of the file I want is the first one with /04 in it...
Thanks.
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

Code: Select all

set data [gets $mkwf]
foreach line [split $data \n] {
 if {[string match "/04*" $line]} { 
 <do something>
 break
}
btw, you might what to close your file too
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

thanks - still no luck - am I doing something else wrong?

bind pub - .cbsnews pub:readmkw
proc pub:readmkw { nick uhost handle channel arg } {
set mkwf [open /home/citinews/Bots/Undernet/news/mkw.txt r]
set data [gets $mkwf]
foreach line [split $data \n] {
if {[string match "/04*" $line]} {
putserv "PRIVMSG #Newsroom2 :$line"
}
}
}
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

use

Code: Select all

set data [read $mkwf] ;#reads entire file at once and sets it to the variable data
i think gets only reads one line

if u use gets use it in a while

Code: Select all

while {![eof $mkwf]} {
  gets $mkwf line
  .... then line is ur varable reads line by line
}
if u do read $mkwf then it will read the entire file

then u split it line by line and output it line by line using foreach
XplaiN but think of me as stupid
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

thanks - it works with this but:

bind pub - .cbsnews pub:readmkw
proc pub:readmkw { nick uhost handle channel arg } {
set mkwf [open /home/citinews/Bots/Undernet/news/mkw.txt r]
set data [read $mkwf]
foreach line [split $data \n] {
if {[string match */04* $line]} {
putserv "PRIVMSG #Newsroom2 :4,4 1,15 [gettime -18]ET $line "
}
}
}

it reads the whole file - can I get it to just read the first /04 line then stop?
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

Code: Select all

# like this u can use it to read any line
# usage: readfile <line number>
proc readfile {number} {
  set count "0"
  set rfile [open "file" "r"]
  while {![$rfile]} {
    gets $rfile line
    set count [expr $count + 1]
    if {"$count" == "$number"} {
      putlog "This is line $number of ur script: $line" ;#change this line to what u want to do with line
      close $rfile 
      return 1 ;#Or change it to return $line if u want the proc to return the line instead then remove the return 0 and putlog line
    }
  }
  close $rfile
  return 0
}
XplaiN but think of me as stupid
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

Code: Select all

proc linematch {var} {
  set rfile [open "file" "r"]
  while {![$rfile]} {
    gets $rfile line
    if {[string match -nocase "*$var*" $line]} {
      # your script here....
      close $rfile
      return 1 ;# This will verrify that the while matched a string
    }
  }
  close $rfile
  return 0
}
hehe sorry misread first time ..
Last edited by Ofloo on Sat Feb 21, 2004 8:10 am, edited 1 time in total.
XplaiN but think of me as stupid
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

thanks but eventually this will be on a timer and not a bindpub so I just need it to read the first line with /04 in it and that's it...thanks!
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

hmm i edited the result cause there where some errors in it .. just wokeup sorry hehe

well u can use it now like this [linematch "/04"]

and if it finds something it will return 1 if it doesn't it will return 0
XplaiN but think of me as stupid
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

thanks - I think I get it - how do I test it by using a pub command? Sorry for all the questions...! :D
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

well hmm

Code: Select all

# usage !test /04 or something else of course only checks arg 0, 
# if you want it to check more then you have to combine stuff or if u
# wana use spaces well then u got to use lrange ..
bind n !test test:pub

proc test:pub {nick uhost hand chan arg} {
  if {[linematch "[lindex $arg 0]"] && ![string match {} [lindex $arg 0]]} {
    putserv "PRIVMSG $chan :Found [lindex $arg 0]." 
  } else {
    putserv "PRIVMSG $chan :Nothing Found."
  }
}
XplaiN but think of me as stupid
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

thanks - I think we're off track here...

I should have explained more - there are lots of lines with /04 in it but I only want it to set the first line of /04 as the variable and send it to the channel...
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
Locked