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 

Max 10 lines in file

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


Joined: 12 May 2009
Posts: 3

PostPosted: Tue May 12, 2009 3:51 am    Post subject: Max 10 lines in file Reply with quote

hay guys
pretty new to tcl scripting, But got myself a add,del and list script going, but i want it so when i do !add text, it checks the file and deletes the oldest entry into the file. So that there is only 10 lines in the file.

So Add entry to Top, Remove last line.

How would i go about doing this?

Cheers in Advanced
Justin
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue May 12, 2009 10:07 am    Post subject: Reply with quote

That kind of depends on what you've done so far, and how you've approached adding/deleting lines.

If you're using read->split approach, it'd be pretty much a matter of adding the new list item and then using lrange to restrict the set to 10 items (lines).
Other approaches would require other techniques..

Post what you've got so far, and we'll go from there..
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
SCR34M3R
Voice


Joined: 12 May 2009
Posts: 3

PostPosted: Wed May 13, 2009 7:04 am    Post subject: Reply with quote

its the sitelist.tcl which i have edited a bit to suit what i need.
the add section is
Code:
## Operation Procedures

proc prc_addsite {site} {
global sitefile
        if {[catch {open $sitefile a} fd]} {
                return 0
        } else {
                puts $fd $site
                close $fd
                return 1
        }
}
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed May 13, 2009 8:42 am    Post subject: Reply with quote

Easiest modification here then, would probably to use list operations..

The approach, roughly put, is to open the file as read-only, and read the whole content. Then use split to convert the raw data into a list.
Once we've got a list, we can use commands such as lappend/linsert and lrange to manipulate the list to suit our needs.
When we're satisfied with our new list, we then use join to restore it to "raw data", and write it back to our file (which now has been opened as write-only).

A rough example:
Code:
set fd [open thefile "RDONLY"]
set tmp [split [read -nonewline $fd] "\n"]
# Read the data, and split it on newlines...

close $fd
set fd [open thefile "WRONLY CREAT TRUNC"]
# Re-open the file in write-only mode, creating the file if it's missing, and clearing any previous content.

puts $fd [join [lrange [linsert $tmp 0 $data] 0 9] "\n"]
# Insert $data at the beginning of the list, then trim the list to the first 10 items, and finally join it back into a textstring, separating each list item with a newline. Write it back into (the empty) file.

close $fd

I've stacked a few steps of the process here, hope you get the picture anyway.

Edit: fixed minor bummer... Thank you papillon for pointing it out.
_________________
NML_375, idling at #eggdrop@IrcNET


Last edited by nml375 on Wed May 13, 2009 9:41 am; edited 1 time in total
Back to top
View user's profile Send private message
Papillon
Owner


Joined: 15 Feb 2002
Posts: 724
Location: *.no

PostPosted: Wed May 13, 2009 9:31 am    Post subject: Reply with quote

Code:
set fd [open thefile "RDONLY"]
set tmp [split [read -nonewline $thefile] "\n"]

should be $fd, as in:
Code:
set fd [open thefile "RDONLY"]
set tmp [split [read -nonewline $fd] "\n"]

_________________
Elen sila lúmenn' omentielvo
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed May 13, 2009 9:40 am    Post subject: Reply with quote

Thnx for pointing it out, I'll update my post in a sec or two.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
SCR34M3R
Voice


Joined: 12 May 2009
Posts: 3

PostPosted: Thu May 14, 2009 3:59 am    Post subject: Reply with quote

awesome, Works a treat.
Thanks heaps guys
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