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.

remove

Old posts that have not been replied to for several years.
Locked
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

remove

Post by caesar »

Code: Select all

"del" {
if {[lindex $text 1] == ""} {
putdcc $idx "\002Usage\002: .randtopic <del> <topic>"
return 0
}

set found 0
set info "[lrange [split $text] 1 end]" 
set file [open $topic(file) r+] 
while {![eof $file]} {
set lines [split [read $file] "\n"]
foreach line $lines { 
if {$line == ""} { continue }
if {$line == $info} { set found 1 } 
}
}
catch {close $file}
if {$found == 1} { 
putdcc $idx "Topic '$info' removed." 
} else { 
putdcc $idx "Topic '$info' not found." 
}
}
How actualy to do the remove part? I know that I must open the file for reading, read all lines from the file except the line I want to be removed and then close the file and open it again for writing, or open it once and do the reading and writing part. I've did some tests and got stucked..
Once the game is over, the king and the pawn go back in the same box.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Your while loop is a bit confused. If you're using [read $file] then that will read in the entire file -- you don't need another loop.

Then your foreach loop can be easily replaced with lsearch and it will make more sense. Right now you set found to 1, but you don't save the line number. How do you know which line to remove?

Finally, you have to rewrite the file, just like you said. To do that, open the file in write mode, and puts -nonewline [join $lines \n].
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Hi, thanks fotr your reply. I've already figured out what I must do and seems that I've succided. I'll follow your instructions and do one and compare wich one is the best one. :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

oh, and here is my code (without your sugestions):

Code: Select all

"del" {
if {[lindex $text 1] == ""} {
putdcc $idx "\002Usage\002: .randtopic <del> <topic>"
return 0
}

set found 0
set index ""
set info "[lrange [split $text] 1 end]" 
set file [open $topic(file) r] 
while {![eof $file]} {
set lines [split [read $file] "\n"]
foreach line $lines { 
if {$line == ""} { continue }
if {$line == $info} {
set found 1
} else { lappend index $line }
}
if {$found == 0} {
putdcc $idx "Topic '$info' not found."
return }
}
catch {close $file}
set file [open $topic(file) w] 
foreach line $index {
puts $file $line
}
catch {close $file}
putdcc $idx "Topic '$info' removed."
}
Once the game is over, the king and the pawn go back in the same box.
Locked