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.

Slowing down pasting of a txt file

Help for those learning Tcl or writing their own scripts.
Post Reply
m
manxmistress
Voice
Posts: 3
Joined: Mon May 07, 2012 3:31 am
Location: Isle of Mann
Contact:

Slowing down pasting of a txt file

Post by manxmistress »

Ok I am ok so far with this but would like to slow down the pasting of each line, what it does is read from a specific txt file and paste on line at a time but is doing so too fast, I would like say 3 or 4 seconds between lines also cant understand why it stops before the end of each file.

Code: Select all

    set tracks [open $filename] 
    set data [split [read -nonewline $tracks] \n] 
    close $tracks 
    foreach trance_line $data { 
        putserv "PRIVMSG $nick :$trance_line" 
    } 
 } 

proc Trance_Listing {nick uhost hand text} {
    set number "trancelist"
    set filename ${::trance_file}/${number}.txt
    set tracks [open $filename] 
    set data [split [read -nonewline $tracks] \n] 
    close $tracks 
    foreach trance_line $data { 
        putserv "PRIVMSG $nick :$trance_line " 
    } 
}
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

There's quite a few ways of doing this; I'd probably do it using a recursive timers, reading one line at a time from the file.

A short example on how you could do this:

Code: Select all

proc tellNextLine {target fd} {
  if {[gets $fd line] >= 0} {
    puthelp "PRIVMSG $target :$line"
    utimer 4 [list tellNextLine $target $fd]
  } else {
    close $fd
  }
}

proc Trance_Listing {nick userhost handle text} {
  tellNextLine $nick [open "${::trance_file}/trancelist.txt" RDONLY]
}
NML_375
m
manxmistress
Voice
Posts: 3
Joined: Mon May 07, 2012 3:31 am
Location: Isle of Mann
Contact:

Post by manxmistress »

Ok i can see what you mean but i dont have a clue how to put that in what I have, that above the user picks the file name and it starts pasting. It would be helpful to know what to do with that in plain english or how it is in relationship to what I have cheers
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Sure,
What I do, is rather than having a foreach-loop iterate through all lines at once, I use timers to repeatedly call my proc named "tellNextLine". This proc then reads the next line from the (already opened) file and sends it to the target, and then starts a new timer (to call tellNextLine again), until there are no more lines remaining in the file ("gets" returns less than 0 bytes read). I then close the file to preserve file descriptors.

Also, rather than read the whole file into memory at once, I leave the file open and only read the one line I need at the moment. Since we're using such a slow pace for sending messages, there's no real benefit from caching the texts in memory.

Other than that, I skipped some temporary variables you've used. The code I wrote should be able to completely replace the code from your original post.
NML_375
m
manxmistress
Voice
Posts: 3
Joined: Mon May 07, 2012 3:31 am
Location: Isle of Mann
Contact:

Post by manxmistress »

Sorry I didn't get back my sub/hubby been ill after chemo so all is well now including what you did thank you so much, :D
Post Reply