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 

Removing lines from text file

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


Joined: 17 May 2007
Posts: 5

PostPosted: Fri May 18, 2007 9:59 am    Post subject: Removing lines from text file Reply with quote

I Have a list in a file called requests.txt which looks like this:
#Test
#Test2
#Test3
#Test4
#Test5
#Test6
(etc)
When i use a !trigger from a channel to remove a certain word from the file it deletes every line in the file instead of the match and here is what i have:
Code:
set fname "files/requests.txt"
set fp [open $fname "w+"]
set data [read -nonewline $fp]
set lines [split $data "\n"]
while {[set i [lsearch -glob $lines $text]]>-1} {
set lines [lreplace $lines $i $i]
}
close $fp


Does anyone know what's going wrong?
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri May 18, 2007 10:55 am    Post subject: Re: Removing lines from text file Reply with quote

blip2 wrote:
Does anyone know what's going wrong?

You truncate the file when you open it (so there will be no data to read) and you never write anything back to the file.

http://tcl.tk/man/tcl8.3/TclCmd/open.htm#M8
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
blip2
Voice


Joined: 17 May 2007
Posts: 5

PostPosted: Fri May 18, 2007 12:29 pm    Post subject: Reply with quote

Code:
set fname "files/requests.txt"
set fp [open $fname "r"]
set data [read -nonewline $fp]
set lines [split $data "\n"]=
close $fp
set fp [open $fname "w"]
while {[set i [lsearch -glob $lines $text]]>-1} {
  set lines [lreplace $lines $i $i]
}
close $fp


now got that but it still deletes all the lines. not just the one that is stated in $text
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Fri May 18, 2007 12:33 pm    Post subject: Reply with quote

You still trunk the file when opening it for writing (which is what you'd most likely want), but you still fail to write anything to it...
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
blip2
Voice


Joined: 17 May 2007
Posts: 5

PostPosted: Fri May 18, 2007 12:41 pm    Post subject: Reply with quote

Code:
set fname "files/requests.txt"
set fp [open $fname "r"]
set data [read -nonewline $fp]
set lines [split $data "\n"]
close $fp
while {[set i [lsearch -glob $lines $text]]>-1} {
  set lines [lreplace $lines $i $i]
}
set fp [open $fname "w"]
puts $fp [join $lines "\n"]
close $fp


seems to be ok now? but does the code look ok? or could it be tidied up a bit?
Back to top
View user's profile Send private message
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