random selection from a set of global variables

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ferhan24badshah
Posts: 35
Joined: Sun Feb 02, 2014 12:20 am

random selection from a set of global variables

Post by ferhan24badshah » Mon Feb 03, 2014 3:44 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: random selection from a set of global variables

Post by dunbarx » Mon Feb 03, 2014 4:33 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: random selection from a set of global variables

Post by dunbarx » Mon Feb 03, 2014 4:37 am

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: random selection from a set of global variables

Post by FourthWorld » Mon Feb 03, 2014 3:14 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply