| View previous topic :: View next topic |
| Author |
Message |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Sat Feb 28, 2009 6:15 pm Post subject: Select a random part of the variable |
|
|
Hey guys, here I am asking for your help --> *again* ^^
I have a variable like this:
| Code: |
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: |
[Sat-10:10:42pm] « @RoBotX » You should have gone here: $bgl
|
Once again, thanks in advance for the help. |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Sat Feb 28, 2009 6:30 pm Post subject: |
|
|
| Code: | | 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: | | set message [lindex $duck(list) [expr {[rand [llength $duck(list) - 2]] + 1}]] |
_________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Feb 28, 2009 6:41 pm Post subject: |
|
|
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: | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Sun Mar 01, 2009 5:26 am Post subject: |
|
|
ahmm I see, so my list is a pseudo-list
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
Once again, I appreciate a lot your help nml375 and speechles
See ya  |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Sun Mar 01, 2009 6:12 am Post subject: |
|
|
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: |
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 :/ |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Sun Mar 01, 2009 8:08 am Post subject: |
|
|
You cannot global only one element of the array, you must global the entire array.
_________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Sun Mar 01, 2009 2:27 pm Post subject: |
|
|
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? |
|
| Back to top |
|
 |
Fill Halfop
Joined: 18 Jan 2009 Posts: 80
|
Posted: Mon Mar 02, 2009 3:54 am Post subject: |
|
|
Done the anti-flood system (based on some other scripts I had here ) |
|
| Back to top |
|
 |
|