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.

Max 10 lines in file

Help for those learning Tcl or writing their own scripts.
Post Reply
S
SCR34M3R
Voice
Posts: 3
Joined: Tue May 12, 2009 3:47 am

Max 10 lines in file

Post by SCR34M3R »

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
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
S
SCR34M3R
Voice
Posts: 3
Joined: Tue May 12, 2009 3:47 am

Post by SCR34M3R »

its the sitelist.tcl which i have edited a bit to suit what i need.
the add section is

Code: Select all

## Operation Procedures

proc prc_addsite {site} {
global sitefile
        if {[catch {open $sitefile a} fd]} {
                return 0
        } else {
                puts $fd $site
                close $fd
                return 1
        }
}
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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: Select all

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.
Last edited by nml375 on Wed May 13, 2009 9:41 am, edited 1 time in total.
NML_375
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

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

Code: Select all

set fd [open thefile "RDONLY"]
set tmp [split [read -nonewline $fd] "\n"] 
Elen sila lúmenn' omentielvo
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Thnx for pointing it out, I'll update my post in a sec or two.
NML_375
S
SCR34M3R
Voice
Posts: 3
Joined: Tue May 12, 2009 3:47 am

Post by SCR34M3R »

awesome, Works a treat.
Thanks heaps guys
Post Reply