| View previous topic :: View next topic |
| Author |
Message |
SCR34M3R Voice
Joined: 12 May 2009 Posts: 3
|
Posted: Tue May 12, 2009 3:51 am Post subject: Max 10 lines in file |
|
|
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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue May 12, 2009 10:07 am Post subject: |
|
|
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 |
|
 |
SCR34M3R Voice
Joined: 12 May 2009 Posts: 3
|
Posted: Wed May 13, 2009 7:04 am Post subject: |
|
|
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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed May 13, 2009 8:42 am Post subject: |
|
|
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 |
|
 |
Papillon Owner

Joined: 15 Feb 2002 Posts: 724 Location: *.no
|
Posted: Wed May 13, 2009 9:31 am Post subject: |
|
|
| 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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed May 13, 2009 9:40 am Post subject: |
|
|
Thnx for pointing it out, I'll update my post in a sec or two. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
SCR34M3R Voice
Joined: 12 May 2009 Posts: 3
|
Posted: Thu May 14, 2009 3:59 am Post subject: |
|
|
awesome, Works a treat.
Thanks heaps guys |
|
| Back to top |
|
 |
|