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

Changing Arrays

Post by smith8867 » Fri Apr 24, 2015 11:59 am

Is there a way to go to a certain level in an array, and change the value?

Thanks

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Changing Arrays

Post by Thierry » Fri Apr 24, 2015 12:05 pm

smith8867 wrote:Is there a way to go to a certain level in an array, and change the value?
Hi Smith,

Code: Select all

put 42 into cheese[1][2][3]
-- or
put 42 into cheese["camembert"]["cheddar"]
is that what you want?

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: Changing Arrays

Post by Klaus » Fri Apr 24, 2015 12:07 pm

Thierry wrote:cheese["camembert"]["cheddar"]
Thin ice, Thierry, very thin ice!
:D :D :D

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

Re: Changing Arrays

Post by smith8867 » Fri Apr 24, 2015 12:08 pm

I'm storing arrays in this form: variable[output]
Basically, I need to go to a certain level in the array and change the value of it. Is that possible?

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

Re: Changing Arrays

Post by Klaus » Fri Apr 24, 2015 12:11 pm

Hi smith8867,

what Thierry said:
...
put "new value" into variable["output"]
...
If that is not what you mean we may need a "real" example of your array,
resp. what you mean with "a certain level of the array".


Best

Klaus

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Changing Arrays

Post by Thierry » Fri Apr 24, 2015 12:11 pm

Klaus wrote: Thin ice, Thierry, very thin ice!
:D :D :D
Nice to see you Klaus :)

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: Changing Arrays

Post by smith8867 » Fri Apr 24, 2015 12:15 pm

Ok so, example:

Run a loop to collect 3 pieces of data.

pupilData[name];
Sam is stored at position 1
Peter is stored at position 2
Jake is stored at position 3

Lets say we choose position 2, is there a way to change "Peter" to something different, without running the loop again?

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

Re: Changing Arrays

Post by Klaus » Fri Apr 24, 2015 12:22 pm

Hm, what do you mean with "position X"?
Something like this:
pupilData["name"][1]["Sam"]
pupilData["name"][2]["Peter"]
pupilData["name"][3]["Jake"]
?

Then use: put Oswald" into pupilData["name"][2]
Result -> pupilData["name"][2]["Oswald"]

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

Re: Changing Arrays

Post by smith8867 » Fri Apr 24, 2015 12:26 pm

Thats what I am looking for, only I am not using that kind of array structure. I am using arrays like this: myVariable[data], Is the method you showed the only way I could do what I am after?

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

Re: Changing Arrays

Post by Klaus » Fri Apr 24, 2015 12:33 pm

smith8867 wrote:...Is the method you showed the only way I could do what I am after?
I may be able to tell you if you can actually explain your array structure! :D
Still have no clue about this...

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

Re: Changing Arrays

Post by dunbarx » Fri Apr 24, 2015 2:15 pm

Hi.

Is it possible that you are asking this question? Put this in a button:

Code: Select all

on mouseUp
   put "firstStuff" into yourArray[firstLevel]
   answer yourArray[firstLevel]
   put "secondStuff" into yourArray[firstLevel][secondLevel]
   answer yourArray[firstLevel][secondlevel]
   put "thirdStuff" into yourArray[firstLevel][secondLevel][thirdLevel]
   answer yourArray[firstLevel][secondLevel][thirdLevel]
   answer yourArray[firstLevel]
end mouseUp
Note that the first answer gives you "firstStuff", but the last answer does not. Is that what you are asking, to find intermediate level values of an array?

Craig Newman

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

Re: Changing Arrays

Post by smith8867 » Fri Apr 24, 2015 5:18 pm

I'll try the best I can to explain.

Code: Select all

on mouseUp
   repeat with loop = 1 to 5
      put "You have not won the prize" into output[loop]
   end repeat
   //
   //Here I want to get a random number, go to that level in the array "output"
   //and change the string to something like "You have won the prize"
end mouseUp
So say the random number generated is 3, it will go to the 3rd string in the array, and change it. Hope that makes sense

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

Re: Changing Arrays

Post by dunbarx » Fri Apr 24, 2015 6:01 pm

Like this?

Code: Select all

on mouseUp
   repeat with loop = 1 to 5
      put "You have not won the prize" into output[loop]
   end repeat

get random(5)
put "You have won the prize" into output[it]
end mouseUp

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

Re: Changing Arrays

Post by smith8867 » Fri Apr 24, 2015 8:16 pm

So if i had to run a loop and output that array using "answer", that wouldn't display 4 non prize winners, and 1 prize winner would it?

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

Re: Changing Arrays

Post by Klaus » Fri Apr 24, 2015 10:13 pm

smith8867 wrote:So if i had to run a loop and output that array using "answer", that wouldn't display 4 non prize winners, and 1 prize winner would it?
EX-ACTLY! :D
smith8867 wrote:So say the random number generated is 3, it will go to the 3rd string in the array, and change it.
Yes:
...
put random(5) into tRandomNumber
put "You have won the prize" into output[tRandomNumber]
...

Best

Klaus

Post Reply