Get a random key from an array

Creating desktop or client-server database solutions?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
madmaxpowers
Posts: 10
Joined: Sat Sep 19, 2015 2:21 am

Get a random key from an array

Post by madmaxpowers » Fri Nov 06, 2015 3:35 pm

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

Klaus
Posts: 12204
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Get a random key from an array

Post by Klaus » Fri Nov 06, 2015 3:59 pm

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

SparkOut
Posts: 2407
Joined: Sun Sep 23, 2007 4:58 pm

Re: Get a random key from an array

Post by SparkOut » Fri Nov 06, 2015 4:02 pm

For readability, three steps:

Code: Select all

put the keys of ateamNames into tKeys
put any line of tKeys into tRandomPick
answer [or put or return...] ateamNames[(tRandomPick)]
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! :)

madmaxpowers
Posts: 10
Joined: Sat Sep 19, 2015 2:21 am

Re: Get a random key from an array

Post by madmaxpowers » Fri Nov 06, 2015 7:02 pm

Thanks A lot!

madmaxpowers
Posts: 10
Joined: Sat Sep 19, 2015 2:21 am

Re: Get a random key from an array

Post by madmaxpowers » Sat Nov 14, 2015 2:54 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5840
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Get a random key from an array

Post by jacque » Sun Nov 15, 2015 1:32 am

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:

Code: Select all

local tArray

on mouseUp -- or any handler name
  answer any line of the keys of tArray
end mouseUp
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:

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

Post Reply

Return to “Databases”