| View previous topic :: View next topic |
| Author |
Message |
darton Op
Joined: 21 Jan 2006 Posts: 155
|
Posted: Tue Jun 06, 2006 2:58 pm Post subject: How to find out if the line exists in textfile |
|
|
Hello!
I made a script that shows me a line of a textfile. For example with "!showline 2" my bot shows the second line of the file. But how can I find out if this line exists, e.g. if the file only has one line? |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Tue Jun 06, 2006 5:06 pm Post subject: |
|
|
Simple, open the file and use a loop to incr a variabile with the number of lines, then close the file and return the number of lines.
| Code: |
proc count:lines {file} {
if {![llength $file]} return
set lines 0
set f [open $file r]
while {[gets $f foo]>-1} {
incr lines
}
close $f
return $lines
}
|
Not tested but should work. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Tue Jun 06, 2006 6:59 pm Post subject: |
|
|
well, I assume when the script can post a specific line, it probably uses an '[lindex $listoflines [expr {$arg + 1}]]' statement. To check in such a case, a simple
| Code: | if {[llength $listoflines] < $arg} {
# no such line
} else {
# ok, lets print
} |
could be used _________________ De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens... |
|
| Back to top |
|
 |
|