| View previous topic :: View next topic |
| Author |
Message |
BCyo+8C4 Voice
Joined: 03 Sep 2006 Posts: 24
|
Posted: Thu Nov 08, 2007 12:34 pm Post subject: selecting a random item from an array |
|
|
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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Nov 08, 2007 1:10 pm Post subject: |
|
|
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 |
|
 |
BCyo+8C4 Voice
Joined: 03 Sep 2006 Posts: 24
|
Posted: Thu Nov 08, 2007 1:33 pm Post subject: |
|
|
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 |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Nov 08, 2007 1:39 pm Post subject: |
|
|
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 |
|
 |
BCyo+8C4 Voice
Joined: 03 Sep 2006 Posts: 24
|
Posted: Thu Nov 08, 2007 1:49 pm Post subject: |
|
|
| 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 |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Thu Nov 08, 2007 4:22 pm Post subject: |
|
|
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!  |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Thu Nov 08, 2007 6:16 pm Post subject: |
|
|
| 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 |
|
 |
r0t3n Owner
Joined: 31 May 2005 Posts: 507 Location: UK
|
Posted: Fri Nov 09, 2007 3:35 pm Post subject: |
|
|
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 |
|
 |
BCyo+8C4 Voice
Joined: 03 Sep 2006 Posts: 24
|
Posted: Sat Nov 10, 2007 6:22 am Post subject: |
|
|
| 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 |
|
 |
|