| View previous topic :: View next topic |
| Author |
Message |
blip2 Voice
Joined: 17 May 2007 Posts: 5
|
Posted: Fri May 18, 2007 9:59 am Post subject: Removing lines from text file |
|
|
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 |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Fri May 18, 2007 10:55 am Post subject: Re: Removing lines from text file |
|
|
| 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 |
|
 |
blip2 Voice
Joined: 17 May 2007 Posts: 5
|
Posted: Fri May 18, 2007 12:29 pm Post subject: |
|
|
| 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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri May 18, 2007 12:33 pm Post subject: |
|
|
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 |
|
 |
blip2 Voice
Joined: 17 May 2007 Posts: 5
|
Posted: Fri May 18, 2007 12:41 pm Post subject: |
|
|
| 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 |
|
 |
|