egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Select a random part of the variable

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sat Feb 28, 2009 6:15 pm    Post subject: Select a random part of the variable Reply with quote

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
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Sat Feb 28, 2009 6:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Feb 28, 2009 6:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sun Mar 01, 2009 5:26 am    Post subject: Reply with quote

ahmm I see, so my list is a pseudo-list Laughing Laughing

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 Rolling Eyes

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

See ya Wink
Back to top
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sun Mar 01, 2009 6:12 am    Post subject: Reply with quote

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
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Sun Mar 01, 2009 8:08 am    Post subject: Reply with quote

Code:
global duck(list)

You cannot global only one element of the array, you must global the entire array.
Code:
global duck

_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Sun Mar 01, 2009 2:27 pm    Post subject: Reply with quote

thanks. worked Smile

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
View user's profile Send private message
Fill
Halfop


Joined: 18 Jan 2009
Posts: 80

PostPosted: Mon Mar 02, 2009 3:54 am    Post subject: Reply with quote

Done the anti-flood system (based on some other scripts I had here Wink )
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber