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.

random text choose??

Old posts that have not been replied to for several years.
Locked
u
up^to^you

random text choose??

Post by up^to^you »

if I have a file named data and contain text like this:

data1*text1*text2*text3

and then I want to execute that file in a tcl
what should I type in that tcl for bot saying one of that text (text1, text2, text3) randomly??

thx
d
darko``
Op
Posts: 121
Joined: Sun Sep 08, 2002 5:33 pm
Location: Malta

Post by darko`` »

Assuming variable $text contains your line:

set mylist [join $text] ### Create a proper list assuming a whitespace is a delimiter
set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]]

The second line looks a bit scary because it need's to get rid of data1 and consider only text1 text2 ect...
Ignorant and lazy people will save 30 minutes by chosing simple config file. Smart ones will save 3000 minutes of *everyone's* time by opting for complete config file.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

darko`` wrote:Assuming variable $text contains your line:

set mylist [join $text] ### Create a proper list assuming a whitespace is a delimiter
set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]]

The second line looks a bit scary because it need's to get rid of data1 and consider only text1 text2 ect...
"join" will create a string from what is supposed to be a list..
What I believe you want is:

Code: Select all

set mylist [split $text]
But since he provided an example line, something like:
data1*text1*text2*text3

It should be:

Code: Select all

set mylist [split $text "*"]
u
up^to^you

Post by up^to^you »

i use : set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]] . and it works. thanks

but there's something bugging me because

if my data filename is contain text like:

data1*a text1*b text2*text3

my eggdrop is only show 'a' and not 'a text1'

and also sometimes dont show any


can you help me find the wrong? thx I really apreciate it
u
up^to^you

Post by up^to^you »

coz

Code: Select all

set mylist [join $text] ### Create a proper list assuming a whitespace is a delimiter 
but I dont use set mylist [join $text]

I just use this code:

Code: Select all

set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]] 

as more detail info the file named data is contain data like this:

data1*text 1*text2*text3
data2*text 4*text5*text 6
..etc
c
cinder
Voice
Posts: 10
Joined: Sun Apr 14, 2002 8:00 pm
Location: Gloucester, UK
Contact:

Post by cinder »

Try:

Code: Select all

set $filename = "data.txt" // Change to filename of data file
set $fs [open $filename r]
gets $fs text
close $fs
set mylist [split $text "*"]
set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]] 
- Cinder
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Code: Select all

set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]] 
RTFM:
rand returns a radon dugit between 0 and <limit>-1
lindex operates on Tcl lists, which have index's starting from 0

If you add 1 onto the random digit, it will produce a blank answer on a random basis.

EG: a 4 item list, where you don't want the first item to be returned.

Thus, using 4 as the random limit, and adding 1, would somtimes produce number 4, which is not a usable list index.

To make this script work correctly, change the following to lines

Code: Select all

set mylist [split $text "*"] 
set myrandomtext [lindex $mylist [expr [rand [llength $mylist]] + 1]]  
to:

Code: Select all

set mylist [lrange [split $text "*"] 1 end]
set myrandomtext [lindex $mylist [rand [llength $mylist]]]  
u
up^to^you

Post by up^to^you »

I try to use:

set mylist [lrange [split $valid($z) "*"] 1 end]
set myrandomtext [lindex $mylist [rand [llength $mylist]]]

but the result is:

[22:48] Tcl error in script for 'timer4':
[22:48] random limit must be greater than zero

in original script, the author used to show all texts like this:

privmsg $chan "left descript are: sentence 1 ... sentence 2... sentence 3 ...

and the code is:

set otherpossibleCounter 0
foreach z [array names valid] {
if {[lindex $valid($z) 0] == "Nobody"} {
if {$otherpossibleCounter != 5} {
append notfound "[lrange $valid($z) 1 end]... "
incr otherpossibleCounter
}
}
}

I dont want to use append and show all text, I just want to show one of them randomly, but how?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

If you get this error, it can only mean what it says.

The random number being generated, is based on 0. Since this number is generated from the length of a list, the length must be 0.

The only way this is possible, is if $valid($z) contatins no * to seperate the fields, or is blank.
u
up^to^you

Post by up^to^you »

i still dont get what you mean :-? :( , can you give me more detail or example of the code that I must write to prevent that..pls? coz I'm not smart as you tought :-?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

It's actualy rather simple, and the english exlanation is that was needed, to say what rthe problem was.

Even for basic programmers, they should know htat simple logic would curb the problem.

Lets see, there was a Tcl error messages, discribing the error message in plain english. It told you that rand's limit can't be 0. If you get this error, then I am guessing the problem is, it is 0.

Now you need to look at where this 0 is coming from. seeing as the parameter for the rand command is "[llength $mylist]", then this is the answer.

Now you can look at why it is returning 0. Well, llength returns the number of elements in a Tcl list. Regardless of the fact the parameter may be a string, if there is text passed to llength, it will return a number above 0.

From this, we now know that $mylist has no contents. It's value is "".

Where does the $mylist variable get set
[lrange [split $valid($z) "*"] 1 end]

So from this deduction, we now know, that this command is returning nothing. Seeing as lrange returns a range of items from a list, those items being the second item onwards, we need to know what is being passed to it.

The split command will send it a list, by splitting a string at the a *. Meaning, if there is no star, no split takes place, and only a list with one item is returned.

So, if only one items is returned, then lrange wont work, as it wants to return the second item onwards (which don't exist).

So simply speaking, a check to see how long the list is would surfice. If it's long enough, then let the rand happen, otherwise don't
Locked