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.

How to find out if the line exists in textfile

Help for those learning Tcl or writing their own scripts.
Post Reply
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

How to find out if the line exists in textfile

Post by darton »

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?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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: Select all

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.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

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: Select all

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