Page 1 of 2

Changing Arrays

Posted: Fri Apr 24, 2015 11:59 am
by smith8867
Is there a way to go to a certain level in an array, and change the value?

Thanks

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:05 pm
by Thierry
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

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:07 pm
by Klaus
Thierry wrote:cheese["camembert"]["cheddar"]
Thin ice, Thierry, very thin ice!
:D :D :D

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:08 pm
by smith8867
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?

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:11 pm
by Klaus
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

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:11 pm
by Thierry
Klaus wrote: Thin ice, Thierry, very thin ice!
:D :D :D
Nice to see you Klaus :)

Regards,

Thierry

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:15 pm
by smith8867
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?

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:22 pm
by Klaus
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"]

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:26 pm
by smith8867
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?

Re: Changing Arrays

Posted: Fri Apr 24, 2015 12:33 pm
by Klaus
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...

Re: Changing Arrays

Posted: Fri Apr 24, 2015 2:15 pm
by dunbarx
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

Re: Changing Arrays

Posted: Fri Apr 24, 2015 5:18 pm
by smith8867
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

Re: Changing Arrays

Posted: Fri Apr 24, 2015 6:01 pm
by dunbarx
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

Re: Changing Arrays

Posted: Fri Apr 24, 2015 8:16 pm
by smith8867
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?

Re: Changing Arrays

Posted: Fri Apr 24, 2015 10:13 pm
by Klaus
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