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 

selecting a random item from an array

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Thu Nov 08, 2007 12:34 pm    Post subject: selecting a random item from an array Reply with quote

Hi,
I've got an array that's populated using
Code:
set players($nick) 1
, the "1" there is increased depending on certain events later.

Now my problem: I want to select a random item (= nick) from the $players array, but I can't figure out how to do it.
Code:
set tnick [lindex $players [rand [llength $players]]]

just tells me later on that can't read "tnick": no such variable and I have no idea what code I'd have to use instead.

Thx for any help.

PS: I only need a random nick from the array, not the number behind it.
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Nov 08, 2007 1:10 pm    Post subject: Reply with quote

players is an array, not a flat variable containing a list... big difference there..
The command you need to use with arrays is called array.
llength only operates on lists, same goes for lindex.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Thu Nov 08, 2007 1:33 pm    Post subject: Reply with quote

I tried various array commands as well, like this one:
Code:
set tnick [[array names $players] [rand [array size $players]]]

I spent about 1,5h trying to figure this out already, the help files don't help me with this anymore. So could you please tell me how to do it?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Nov 08, 2007 1:39 pm    Post subject: Reply with quote

Have a second look at "array names", and you should see that it returns a list of keys in the array. That on it's own is quite unusable as a command.
But, since you now got a list of names, make use of commands such as lindex to actually select an item from that list...
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Thu Nov 08, 2007 1:49 pm    Post subject: Reply with quote

seriously, I already played ages with those commands and couldn't figure it out. if you want to help me please do so, but don't make me look like someone who doesn't rtfm!
Back to top
View user's profile Send private message
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Thu Nov 08, 2007 4:22 pm    Post subject: Reply with quote

In other words, get the array names into a list variable, then pull the random name you want, then use that name to get the data from the array! Smile
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Thu Nov 08, 2007 6:16 pm    Post subject: Reply with quote

Code:
set playersList [array names players]
set tnick [lindex $playersList [rand [llength $playersList]]]


Edit: Thanks Tosser^^
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts


Last edited by Sir_Fz on Fri Nov 09, 2007 8:15 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
r0t3n
Owner


Joined: 31 May 2005
Posts: 507
Location: UK

PostPosted: Fri Nov 09, 2007 3:35 pm    Post subject: Reply with quote

a little error there Sir_Fz, forgot the rand...

Code:
set playersList [array names players]
set tnick [lindex $playersList [rand [llength $playersList]]]


or in one line:

Code:
set tnick [lindex [array names players] [rand [array names players]]]

_________________
r0t3n @ #r0t3n @ Quakenet
Back to top
View user's profile Send private message MSN Messenger
BCyo+8C4
Voice


Joined: 03 Sep 2006
Posts: 24

PostPosted: Sat Nov 10, 2007 6:22 am    Post subject: Reply with quote

thanks guys. I had already tried that method before but due to an error later in the script the $tnick wouldn't be picked up (strangely it was when I had set it manually, that's why I didn't figure it at first)
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