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 

Problem with deleting a whole line of a 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: Wed Aug 09, 2006 5:16 pm    Post subject: Problem with deleting a whole line of a textfile Reply with quote

Hello!
In the meantime I have managed many things with eggdrop <-> textfiles. But with deleting a line out of a textfile I have a problem. You can see my script here:
Code:
bind pub - !delop delop
proc delop {nick uhost hand chan arg} {
global adminfile
 if {$arg != ""} {
  set op [lindex [split $arg] 0]
  set fd [open $::adminfile r+]
      while {![eof $fd]} {
         lappend list [gets $fd]         
      }         
     if {[set le [lsearch -exact $list $op]] != -1} {
          #The following line should do the delete stuff, but doesn't
          set list [lreplace $list $le $le]
          puts -nonewline $fd [join $list \n]
          close $fd
        } else {
          putquick "PRIVMSG $chan :Your moderator does not exist."
        }
 }
}

According to the "Basic File Operations" Thread of stdragon a line should be deleted by the following code: "set lines [lreplace $lines $line_to_delete $line_to_delete]". De_Kus said to me that the variable $list and $lines are equivalent. With the line "[set le [lsearch -exact $list $op]]" I am looking for the name $op in the textfile and I am saving the line into the variable $le. So $le and $line_to_delete should be the same, too. But it does not work. The content in the textfile is simply copied and pasted again.
Can anybody tell me where the mistake is?


Last edited by darton on Fri Aug 11, 2006 11:56 am; edited 1 time in total
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Aug 09, 2006 5:29 pm    Post subject: Reply with quote

$lines and $list are not the same...
lreplace can't alter $list, as it only sees it as variable content, rather than the variable itself. Basically, you could use it without variable aswell, ie:
Code:
set mylist [lreplace [list a b c d e] 3 4]

Try something like this:
Code:

bind pub - !delop delop
proc delop {nick uhost hand chan arg} {
 global adminfile
 if {$arg != ""} {
  set op [lindex [split $arg] 0]
  set fd [open $::adminfile r+]
  while {![eof $fd]} {
   lappend list [gets $fd]         
  }         
  if {[set le [lsearch -exact $list $op]] != -1} {
   puts -nonewline $fd [join [lreplace $list $le $le] \n]
   close $fd
  } else {
   putquick "PRIVMSG $chan :Your moderator does not exist."
  }
 }
}

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Wed Aug 09, 2006 5:43 pm    Post subject: Reply with quote

Exactly the same happens. Another suggestion?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Aug 09, 2006 5:58 pm    Post subject: Reply with quote

Actually, when you open the file with r+, any writes to the file will append the new data, ie. the file is not truncated pre-write..
close it after you've read it, and open it with w+ when you wish to write to it..
Code:

bind pub - !delop delop
proc delop {nick uhost hand chan arg} {
 global adminfile
 if {$arg != ""} {
  set op [lindex [split $arg] 0]
  set fd [open $::adminfile r+]
  while {![eof $fd]} {
   lappend list [gets $fd]         
  }
  close $fd
  set fd [open $::adminfile w+]
  if {[set le [lsearch -exact $list $op]] != -1} {
   puts -nonewline $fd [join [lreplace $list $le $le] \n]
  } else {
   putquick "PRIVMSG $chan :Your moderator does not exist."
  }
  close $fd
 }
}

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Wed Aug 09, 2006 6:17 pm    Post subject: Reply with quote

It works perfectly now. Thank you.
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Thu Aug 10, 2006 5:52 am    Post subject: Reply with quote

By the way, is it possible to delete more than one line?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Aug 10, 2006 9:14 am    Post subject: Reply with quote

Yep.
If the lines are consecutive, just alter the "last"-argument of lreplace, and it'll replace all elements between "first" and "last" (inclusive).

If the lines are spread out however, you'll have to use multiple lreplace's.
In this case I'd suggest you'd rewrite the script slightly in order to store the result from each lreplace in a variable inbetween the lreplace operations..
ie:
Code:

set dellist [list "lines" "to" "be" "removed"]
...
foreach op $dellist {
 if {[set le [lsearch -exact $list $op]] != -1} {
  set list [lreplace $list $le $le]
 }
}
puts -nonewline $fd [join $list "\n"]

Oh, a word of advice, there is a chance that your file will be cleared if you try to remove a line that's not in the file in the example I wrote earlier (my bad). A simple solution would be to only open and close the file a second time if the lsearch is successful (just move the open and close-command inside the if-statement)
Code:

  if {[set le [lsearch -exact $list $op]] != -1} {
   set fd [open $::adminfile w+]
   puts -nonewline $fd [join [lreplace $list $le $le] \n]
   close $fd
  } else {
   putquick "PRIVMSG $chan :Your moderator does not exist."
  }

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Thu Aug 10, 2006 12:20 pm    Post subject: Reply with quote

I've said that countless times before, but darton just won't listen.

The (arguably) proper way of using files in eggdrop scripts is:

  • open the file only ONCE, on script's start-up; read the entire content into a list (or array or whatever), then close the file
  • manipulate that list as you wish in your event handlers
  • periodically SAVE the list into file


I've yet to see an eggdrop script having to handle files so large that the above won't work
_________________
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code
Back to top
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Aug 10, 2006 12:36 pm    Post subject: Reply with quote

I'd guess that'd be the only way of doing it, unless you'd like to mess with temp-files..
Oh well, there is random-access mode, but unless you're comfortable coding fixed-size datastructures with lazy-delete, you really don't wanna touch that one Wink
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Thu Aug 10, 2006 2:01 pm    Post subject: Reply with quote

demond wrote:
I've said that countless times before, but darton just won't listen.

I am not good in making proper scripts. My scripts work but aren't proper.Wink

But the script above is proper, isn't it?
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