I am trying to count the number of lines in a file. The script i have so far is:
set number 0
set fs [open $requestpassfile r]
while {![eof $fs]} {
foreach line [gets $fs] {
set count [expr $number+1]
}
}
close $fs
putnotc $nick "There are $count password requests pending"
this script doesnt work though, it only ever says there are 1 lines in the file.
Are you sure its reading the file correctly? Try adding a putlog $line or another debug way to actually see what its counting. Off of the top of my head, I would have to say that script appears to be correct.
hm actually, i just put a putlog "test" underneath 'foreach line [gets $fs] {' and it resulted in repeating 'test' 16 times. I have looked at the file manually and it only has 1 line in it...
proc msg_listpassreqs {nick uhost hand args} {
global botnick requestpassfile
set fileread [open $requestpassfile r]
set all [read $fileread]
close $fileread
if { $all == "" } {
putnotc $nick "Error: No password requests pending"
return 0
}
set number 0
foreach count [split $all \n] {
incr number
}
putnotc $nick "There are [expr $number-1] password requests pending"
foreach line [split $all \n] {
puthelp "NOTICE $nick :$line"
}
}
just a tip, since you are converting "all" to a list delimited by \n, you might want to use llength to save iterations/cpu cycles. (ie. set number [llength [split $all \n]])
On the shell system I'm using "[exec cat $filename | wc -l]" is about 15 times as slow as the "open/read/close/split/llength".
Your suggestion can be improved a bit by using [exec wc -l $filename], but it is still a factor 10 slower.