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.

Select a random part of the variable

Help for those learning Tcl or writing their own scripts.
Post Reply
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Select a random part of the variable

Post by Fill »

Hey guys, here I am asking for your help --> *again* ^^

I have a variable like this:

Code: Select all

set duck(list) {
"Message 1"
"Message 2"
"Message 3"
"Message 4"
"Message 5"
"Message 6"
"Message 7"
"Message 8"
"Message 9"
"Message 10"
"Message 11"
etc etc...
}
How can I make the bot read RANDOMLY one of the messages in variable $duck(list)?
Oh, and no, I can't have the messages in a *.txt file, because those messages have other variables in their content, so if they were saved in a file, the variables wouldn't be read and something like this would happen:

Code: Select all

[Sat-10:10:42pm] « @RoBotX » You should have gone here: $bgl
Once again, thanks in advance for the help.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

while {![string length [set message [lindex $duck(list) [rand [llength $duck(list)]]]]]} { }
That would do it. The main reason to use the while and checking for string length is because the first and last entries in your pseudo list are going to be empty strings (nulls) and obviously we want to avoid those. This is why I'm nesting an empty field after the while, because we are using the set command to set the message within the while statement, so the loop which runs really doesn't need to do anything (it's why its { }) except run the while check over and over until we have a string with length to it.

Conversely, you can use expr to remove the first and last elements from the length of your pseudo list (-2) and then run rand over that and add one (+1) to the outcome which will work exactly as the while loop does above, skipping the first and last empty entries.

Code: Select all

set message [lindex $duck(list) [expr {[rand [llength $duck(list) - 2]] + 1}]]
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Another workaround would be to define the list properly, in which case you would'nt get any empty elements (yet perhaps a bit uglier code)

Code: Select all

set duck(list) [list \
"Message 1" \
"Message 2" \
"Message 3" \
"Message 4" \
"Message 5" \
"Message 6" \
"Message 7" \
"Message 8" \
"Message 9" \
"Message 10" \
"Message 11" \
]

set message [lindex $duck(list) [rand [llength $duck(list)]]]
NML_375
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

ahmm I see, so my list is a pseudo-list :lol: :lol:

A normal and correct list would be something like nml375 posted? Thanks for the tips. I guess I'll try to make a REAL list, I didn't know my list was incorrect :roll:

Once again, I appreciate a lot your help nml375 and speechles

See ya ;)
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

got an error message with the created list:

[Sun-10:13:01am] « RoBotX » [10:13] Tcl error [testing]: bad variable name "duck(list)": upvar won't create a scalar variable that looks like an array element

It is now like this:

Code: Select all

set duck(list) [list \
"Message 1" \
"Message 2" \
"Message 3" \
"Message 4" \
"Message 5" \
"Message 6" \
"Message 7" \
"Message 8" \
"Message 9" \
"Message 10" \
"Message 11" \
]

proc testing { nick uhost hand chan args } {
global duck(list)
set message [lindex $duck(list) [rand [llength $duck(list)]]]
puthelp "PRIVMSG $chan :\001ACTION $message"
}
But I get that message in dcc session :/
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

global duck(list) 
You cannot global only one element of the array, you must global the entire array.

Code: Select all

global duck
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

thanks. worked :)

Now I'd like to add an anti-flood system.

This works with a command (!duck), and I want the bot to set a 5 minutes ignore if a certain host (*!*@host) sends more than 1 !duck in 10 seconds. How can I make this?
F
Fill
Halfop
Posts: 80
Joined: Sun Jan 18, 2009 6:08 pm

Post by Fill »

Done the anti-flood system (based on some other scripts I had here :wink: )
Post Reply