egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to find out if the line exists in textfile

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Tue Jun 06, 2006 2:58 pm    Post subject: How to find out if the line exists in textfile Reply with quote

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
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Tue Jun 06, 2006 5:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Tue Jun 06, 2006 6:59 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber