Get a random key from an array
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 10
- Joined: Sat Sep 19, 2015 2:21 am
Get a random key from an array
Hello
I am attempting to "answer" with a random from a list or an array
What would the syntax to access an array and select a random key look like?
Get random key of ateamNames [ ]
Or would I need a repeat statement to iterate through the array?
Thanks
I am attempting to "answer" with a random from a list or an array
What would the syntax to access an array and select a random key look like?
Get random key of ateamNames [ ]
Or would I need a repeat statement to iterate through the array?
Thanks
Re: Get a random key from an array
Hi Max,
use ANY when dealing with text lists:
...
put the keys of tArray into tKeys
put any line of tKeys into tRandomKey
answer tArray[tRandomkey]
...
Best
Klaus
use ANY when dealing with text lists:
...
put the keys of tArray into tKeys
put any line of tKeys into tRandomKey
answer tArray[tRandomkey]
...
Best
Klaus
Re: Get a random key from an array
For readability, three steps:you can condense this if you like, but this shows what steps you have to use. You can get away without the parentheses inside the square brackets but it does ensure the engine resolves the variable to use for the key. If you have constructions like "Text" & tVar to be concatenated, it is important to use the parentheses, so a common habit of mine.
Edit: fah, Klausimausi gets there first!
Code: Select all
put the keys of ateamNames into tKeys
put any line of tKeys into tRandomPick
answer [or put or return...] ateamNames[(tRandomPick)]
Edit: fah, Klausimausi gets there first!
-
- Posts: 10
- Joined: Sat Sep 19, 2015 2:21 am
Re: Get a random key from an array
Thanks A lot!
-
- Posts: 10
- Joined: Sat Sep 19, 2015 2:21 am
Re: Get a random key from an array
okay I tried it and it can't make it work. I'm trying hard to learn through experience without just asking you guys to do it for me but I'm having problems with this one.
I think its something to do with how and where I declare the variables. Is it possible to explain where i would store my variables then more importantly explain why they would go there?
Edit*
I created a new stack for the sole purpose of trying to figure out how this works.
I placed a single button in middle of new stack
I put this exactly into the script of the BUTTON
local tArray
local tKeys
local tRandomKey
put "Hello." into tArray [1]
put "Hello2." into tArray [2]
put "Hello3." into tArray [3]
put "Hello4." into tArray [4]
on mouseUp
put the keys of tArray into tKeys
put any line of tKeys into tRandomKey
answer tArray[tRandomkey]
end mouseUp
//
After clicking the button it answers with nothing in the answer which is what was happening when it tried on my old project.
Also if you put the keys into tKeys then into tRandomKeys why would you need to answer with tArrray and tRandomKey ( not critiquing I'm attempting to understand)
THANKS
//something else I tried//
Also i noticed online it doesn't say you have to declare array before putting stuff into into it so I deleted the local tArray declaration at the top and now get an error of "unquoted literal' here tArray is accessed in the mouse up section
I think its something to do with how and where I declare the variables. Is it possible to explain where i would store my variables then more importantly explain why they would go there?
Edit*
I created a new stack for the sole purpose of trying to figure out how this works.
I placed a single button in middle of new stack
I put this exactly into the script of the BUTTON
local tArray
local tKeys
local tRandomKey
put "Hello." into tArray [1]
put "Hello2." into tArray [2]
put "Hello3." into tArray [3]
put "Hello4." into tArray [4]
on mouseUp
put the keys of tArray into tKeys
put any line of tKeys into tRandomKey
answer tArray[tRandomkey]
end mouseUp
//
After clicking the button it answers with nothing in the answer which is what was happening when it tried on my old project.
Also if you put the keys into tKeys then into tRandomKeys why would you need to answer with tArrray and tRandomKey ( not critiquing I'm attempting to understand)
THANKS
//something else I tried//
Also i noticed online it doesn't say you have to declare array before putting stuff into into it so I deleted the local tArray declaration at the top and now get an error of "unquoted literal' here tArray is accessed in the mouse up section
-
- VIP Livecode Opensource Backer
- Posts: 7237
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Get a random key from an array
The lines that create the array won't run if they aren't inside any handler declaration. So these do nothing:
put "Hello." into tArray [1]
put "Hello2." into tArray [2]
put "Hello3." into tArray [3]
put "Hello4." into tArray [4]
You need an "on something" and "end something" around those lines. In your real working script, you need the same thing, as well as declaring the local variable at the top of the script so that its content will be seen by all the other handlers.
Also, it sounds like you don't really want the values of the array keys, you just want the name of the key itself. So all you need is this:
If you can post enough of your working script to show us how it is structured, we can help you arrange it. In your button test, it might look like this:
put "Hello." into tArray [1]
put "Hello2." into tArray [2]
put "Hello3." into tArray [3]
put "Hello4." into tArray [4]
You need an "on something" and "end something" around those lines. In your real working script, you need the same thing, as well as declaring the local variable at the top of the script so that its content will be seen by all the other handlers.
Also, it sounds like you don't really want the values of the array keys, you just want the name of the key itself. So all you need is this:
Code: Select all
local tArray
on mouseUp -- or any handler name
answer any line of the keys of tArray
end mouseUp
Code: Select all
local tArray
on makeArray
put "Hello." into tArray [1]
put "Hello2." into tArray [2]
put "Hello3." into tArray [3]
put "Hello4." into tArray [4]
end makeArray
on mouseUp
makeArray
answer any line of the keys of tArray
end mouseUp
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com