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
random selection from a set of global variables
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 35
- Joined: Sun Feb 02, 2014 12:20 am
Re: random selection from a set of global variables
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:
Click the button a few times. You get the picture.
Craig Newman
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
Craig Newman
Re: random selection from a set of global variables
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
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
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: random selection from a set of global variables
Circumstances where you need to access variables whose names cannot be known in advance are a natural fit for LiveCode's arrays:
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.
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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn