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.

Read from .txt file...

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Read from .txt file...

Post by DjBeNNeTT »

I've done a search, not got far.. i did find this so i tried to sort it out, but because i cant code TCL alot yet, it obviously it isnt working.

Any ideas? Would help alot :)

By The way, its to read from a file called sex.txt and to choose a random line on !sex to say into the #chan.

Code: Select all

set sex "./sex.txt"
set sexmsg [string range [randomline $sex] 0 end]

bind pub - !sex randomline

proc randomline {nick uhost hand chan arg} {
 # !sex triggers this procedure.

proc randomline { sex } {

  set position [rand [expr [file size $filename] - 1024]]

  set fd [open $filename r]
  catch {
    seek $fd $position
    set text [read $fd 1024]
  }
  close $fd

  set lines [split $text \n]
  set lineno [randrange 1 [expr [llength $lines] - 1]]

  return [lindex $lines $lineno]
}
}
Last edited by DjBeNNeTT on Sat Apr 22, 2006 5:44 pm, edited 1 time in total.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

- you have pasted the code twice
- you define a proc within a proc
- you define two procs with the same name
- you unncessarily open the file twice (use gets twice, instead of read and second open)
- you define the random message globally... it will stay the same until rehash/restart

thats about all I can say about that code which will never work as itented like it is :D.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Post by DjBeNNeTT »

Well as i said, i know little. So i was wondering of some help.

Sorry for pasting twice, its now just once.

I had guessed that the 2 proc was wrong.. the first one was which causing the error.

Thanks for noticing tho's... Hope someone can point me in the right (coding) direction ;)
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

Code: Select all

set sex "./sex.txt" 

bind pub - !sex sex:msg

proc sex:msg {nick uhost hand chan arg} {
  global sex
  set sexmsg [string range [randomline $sex] 0 end]
  puthelp "privmsg $chan :$sexmsg"
}

proc randomline { filename } {

  set position [rand [expr [file size $filename] - 1024]]

  set fd [open $filename r]
  catch {
    seek $fd $position
    set text [read $fd 1024]
  }
  close $fd

  set lines [split $text \n]
  set lineno [randrange 1 [expr [llength $lines] - 1]]

  return [lindex $lines $lineno]
}

proc randrange { lowerbound upperbound } {
 return [expr {int(($upperbound - $lowerbound + 1) * rand() + $lowerbound)}]
}
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Post by DjBeNNeTT »

Your a god for loads of stuff i want if this works!!!
Just the one prob i find atm...

on !sex...
[23:30:51] [BooByBoT] [22:30] Tcl error [sex:msg]: random limit must be greater than zero

I've changed both "set sexmsg [string range [randomline $sex] 0 end] " and "* rand()" to (3) and still nothing...
I'm turning blind (must be tired) lol
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

change:

Code: Select all

set sex "./sex.txt"
to

Code: Select all

set sex "scripts/sex.txt"
.. making certain the file (sex.txt) actually exists and you have added the lines of text you want (using pico).

I tested on a Windrop and it worked 100%. :)
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Post by DjBeNNeTT »

Mines windrop and Still getting same message...

my sex.txt file has:
Ohhh babeh!
Hmmmm!
YES YES YEEESSHHHH!!!!
With You? [censored] off!
Send Me Your Pic First ;)
Get Away From Me With That Filthy Anaconda!
All as it looks there..
What you mean "using pico"?
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Code: Select all

set sex "./sex.txt"

bind pub - !sex sex:msg

proc sex:msg {nick uhost hand chan arg} {
  global sex
  set sexmsg [string range [randomline $sex] 0 end]
  puthelp "privmsg $chan :$sexmsg"
}

proc randomline f {
 set data [split [read [set file [open $f]]][close $file] \n]
 set position [rand [llength $data]]
 lindex $data $position
}
But the best solution would be to read the file into a list and use the list instead of the file everytime the command is called. Try to search the forum about how that can be implemented, it has been implemented 100 times here.
Last edited by Sir_Fz on Sat Apr 22, 2006 7:48 pm, edited 1 time in total.
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

Excellent Sir_Fz. :)

I concur. I should have thought of that in the first place. :lol:
DjBeNNeTT wrote:What you mean "using pico"?
pico is a *nix text editor. I was unaware you were using a Windrop in which case you'd be using notepad/EditPlus to edit scripts.
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Post by DjBeNNeTT »

Thank you both for your time. Sir_Fz worked perfectly! Thanks :D

And i actually use WordPad for scripts... as Notepad likes to bugger up most the time and not set out the next lines properly making it unreadable... Bugger thing.

Ah well

Thanks both again :D
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Post by DjBeNNeTT »

Thanks, i'll give it a try.

Is there a way that if it has $nick in the .txt file it will say the nickname who did the command? Or can that only be done in the code itself to say for each message?.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

use

Code: Select all

[subst -nocommands $sexmsg]
instead of just $sexmsg.
D
DjBeNNeTT
Voice
Posts: 27
Joined: Sat Feb 18, 2006 6:40 pm
Location: Telford, Shropshire, UK
Contact:

Post by DjBeNNeTT »

:oops: where :oops:

LoL sorry :p
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

How many $sexmsgs are there in that code?
Post Reply