Changing Arrays

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

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Changing Arrays

Post by smith8867 » Tue Apr 28, 2015 10:00 am

Ok I have found a better solution to my issue.

How would I be able to get a random value from the array: users[loop] ?

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

Re: Changing Arrays

Post by dunbarx » Tue Apr 28, 2015 4:24 pm

Hi.
How would I be able to get a random value from the array: users[loop] ?
You cannot get a random number from an element of an array, you have to put a random number into an element of an array. I suspect I am not understanding what you need. Put this in a button:

Code: Select all

on mouseUp
   repeat with y = 1 to 5
      put random(999) into myArray[y]
   end repeat
end mouseUp
Put a breakpoint at the last line. You will see five random numbers in the array. If you want a (random) one of these values, use Klaus' suggestion a post or two back. But is this what you meant?

Craig

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

Re: Changing Arrays

Post by SparkOut » Tue Apr 28, 2015 7:13 pm

Maybe something like

Code: Select all

put the keys of users into tKeys
put any line of tKeys into tChosenOne
put users[(tChosenOne)]
?????
Echoing Craig, but is this what you meant?

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Changing Arrays

Post by smith8867 » Thu Apr 30, 2015 1:34 pm

Perhaps I am unclear :/
Heres what I am looking to achieve:
- Enter all users, store it in users[loop]
- Get a random name from the array users[loop]
- Output the name that was randomly chosen

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing Arrays

Post by Klaus » Thu Apr 30, 2015 2:01 pm

We still don't have any clue how you organize your array, means what is in what key!
We need to know before we finally can answer your question!

You are obviously NOT using any array form/format we have been discussed so far 8)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Changing Arrays

Post by jacque » Thu Apr 30, 2015 4:43 pm

SparkOut had the answer. Get a random key and output its value.

I don't really think you need an array for this though, a simple list would be enough unless you plan to add more data to the array later.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Changing Arrays

Post by smith8867 » Fri May 08, 2015 11:51 am

Appologies for not understanding. They are being stored like so:

Code: Select all

repeat with loop = 1 to 5
ask "Please enter the users name"
put it into name[loop]
end repeat
I would like to choose a random "user" from the "name[loop]" array.I am using the usernames later on in the program.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Changing Arrays

Post by Klaus » Fri May 08, 2015 11:59 am

Hi smith8867,

1. do NOT use reserved (by Livecode) words as variable names!
NAME is e.g. a reserved word, you will get in trouble later on :D

2. We already gave you the answer to your last question, see SparkOuts last posting!
HInt: HIs ARRAY is named "users" 8)


Best

Klaus

Post Reply