Page 1 of 1
random selection from a set of global variables
Posted: Mon Feb 03, 2014 3:44 am
by ferhan24badshah
I have a set of global variables before a handler
Within the handler, How do i randomly select one of the global variables and retrieve its content? It would also be nice if the random selection functioned in a way where the no variable is selected twice in a row.
Thanks
Re: random selection from a set of global variables
Posted: Mon Feb 03, 2014 4:33 am
by dunbarx
Hi.
I would try to make life easy by setting up my globals in an accessible way, like:
gVar1
gVar2
gVar3
and so you might try this in a button script:
Code: Select all
on mouseup
global gVar1,gVar2,gVar3
put 1 into gVar1
put 2 into gVar2
put 3 into gVar3
do "answer" && gVar & random(3)
end mouseup
Click the button a few times. You get the picture.
Craig Newman
Re: random selection from a set of global variables
Posted: Mon Feb 03, 2014 4:37 am
by dunbarx
Hi again.
Just reread your query about making sure that no duplicates would come out of a sequence of random selections. You can always use the "deleteVariable" command. Look this up in the dictionary.
But now I am thinking that the use of globals for all this processing is both excessive and unwarranted. Can't the whole shebang be migrated to some sort of more ordinary data storage, perhaps a custom property?
Craig
Re: random selection from a set of global variables
Posted: Mon Feb 03, 2014 3:14 pm
by FourthWorld
Circumstances where you need to access variables whose names cannot be known in advance are a natural fit for LiveCode's arrays:
Code: Select all
global gArray
on mouseUp
LoadArray
AccessRandomElementInArray
end mouseUp
on LoadArray
put "Howdy" into gArray["Bill"]
put "Hello" into gArray["Steve"]
put "Halo" into gArray["Klaus"]
put "Bonjour" into gArray["Pierre"]
put "Ni Hao" into gArray["Andy"]
end LoadArray
on AccessRandomElementInArray
put gArray[any line of the keys of gArray]
end AccessRandomElementInArray
Note that the "any" operator can be used for random selection within a chunk, sometimes more convenient that using the random function and accessing lines by the resulting number.