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.

Removing lines from text file

Help for those learning Tcl or writing their own scripts.
Post Reply
b
blip2
Voice
Posts: 5
Joined: Thu May 17, 2007 11:39 am

Removing lines from text file

Post by blip2 »

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

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?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: Removing lines from text file

Post by user »

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"?
b
blip2
Voice
Posts: 5
Joined: Thu May 17, 2007 11:39 am

Post by blip2 »

Code: Select all

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
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
b
blip2
Voice
Posts: 5
Joined: Thu May 17, 2007 11:39 am

Post by blip2 »

Code: Select all

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