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.
Help for those learning Tcl or writing their own scripts.
BCyo+8C4
Voice
Posts: 24 Joined: Sun Sep 03, 2006 11:11 am
Post
by BCyo+8C4 » Thu Nov 08, 2007 12:34 pm
Hi,
I've got an array that's populated using
, 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: Select all
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.
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Thu Nov 08, 2007 1:10 pm
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
BCyo+8C4
Voice
Posts: 24 Joined: Sun Sep 03, 2006 11:11 am
Post
by BCyo+8C4 » Thu Nov 08, 2007 1:33 pm
I tried various array commands as well, like this one:
Code: Select all
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?
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Thu Nov 08, 2007 1:39 pm
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
BCyo+8C4
Voice
Posts: 24 Joined: Sun Sep 03, 2006 11:11 am
Post
by BCyo+8C4 » Thu Nov 08, 2007 1:49 pm
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!
rosc2112
Revered One
Posts: 1454 Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania
Post
by rosc2112 » Thu Nov 08, 2007 4:22 pm
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!
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Thu Nov 08, 2007 6:16 pm
Code: Select all
set playersList [array names players]
set tnick [lindex $playersList [rand [llength $playersList]]]
Edit: Thanks Tosser^^
Last edited by
Sir_Fz on Fri Nov 09, 2007 8:15 pm, edited 1 time in total.
r0t3n
Owner
Posts: 507 Joined: Tue May 31, 2005 6:56 pm
Location: UK
Post
by r0t3n » Fri Nov 09, 2007 3:35 pm
a little error there Sir_Fz, forgot the rand...
Code: Select all
set playersList [array names players]
set tnick [lindex $playersList [rand [llength $playersList]]]
or in one line:
Code: Select all
set tnick [lindex [array names players] [rand [array names players]]]
r0t3n @ #r0t3n @ Quakenet
BCyo+8C4
Voice
Posts: 24 Joined: Sun Sep 03, 2006 11:11 am
Post
by BCyo+8C4 » Sat Nov 10, 2007 6:22 am
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)