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.

Counting the number of lines in a file

Old posts that have not been replied to for several years.
Locked
s
simonbell
Halfop
Posts: 68
Joined: Mon Aug 05, 2002 8:07 pm
Location: Washington, England
Contact:

Counting the number of lines in a file

Post by simonbell »

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.

what am i doing wrong?

thanks
Simon
t
tainted
Master
Posts: 239
Joined: Sun May 12, 2002 8:00 pm
Location: chicago
Contact:

Post by tainted »

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.
s
simonbell
Halfop
Posts: 68
Joined: Mon Aug 05, 2002 8:07 pm
Location: Washington, England
Contact:

Post by simonbell »

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

Simon
s
simonbell
Halfop
Posts: 68
Joined: Mon Aug 05, 2002 8:07 pm
Location: Washington, England
Contact:

Post by simonbell »

I have sorted the problem now:
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"
}
}
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

simonbell wrote:I have sorted the problem now:
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]])
M
Moo_Moo

Counting the lines in a file

Post by Moo_Moo »

Why not just do the following?

Code: Select all

set filelength [exec cat $filename | wc -l]
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Re: Counting the lines in a file

Post by egghead »

Moo_Moo wrote:Why not just do the following?

Code: Select all

set filelength [exec cat $filename | wc -l]
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.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: Counting the lines in a file

Post by strikelight »

Moo_Moo wrote:Why not just do the following?

Code: Select all

set filelength [exec cat $filename | wc -l]
And not all systems "need" to have 'cat' or 'wc' commands. (ie. Windows).
Locked