Editing Array Values: Solution
4 posts • Page 1 of 1
Editing Array Values: Solution
Lots of people have needed to edit specific values in an array. I finally decided to make a function to do so.
Here it is:
You may notice the ridiculous amount of arguments, but they are all necessary, due to restrictions in thyme
The arguments are pos, value, array, arrayLength and arrayString.
pos: The value you want to change, e.g. array = [0,1,2] if i wanted to change the 1, the pos would be 1, because it starts from 0, counting upwards. so the 0 is array(0) and so on. Like if you were reading the array with Thyme.
value: What you want to change the value at pos to. Fairly self explanatory.
array: Even more self explanatory. e.g. scene.my.exampleArray
arrayLength: This is confusing; it is the length of the array, but starting from 1 for the first value. So the array [1,0,0, 1,0,1] would have a length of 6.
arrayString: This seems unnecessary, but is essential. It is just the same as what you put in as the array argument, just as a string. So , if you put scene.my.arrayExample as the argument for array, then this would be "scene.my.arrayExample". Or, any other array you want to read the end value into, but remember the quotes.
I realise i don't need the array argument, with the arrayString argument, but i am to lazy to fix this
Hope this helps!
EDIT:
Oops, forgot an example:
Here it is:
- Code: Select all
scene.my.arrayEdit = (pos, value, array, arrayLength, arrayString)=>{ scene.my.tempArray = []; for(pos, (i)=>{scene.my.tempArray = scene.my.tempArray ++ [array(i)]}); scene.my.tempArray = scene.my.tempArray ++ [value]; for(arrayLength - (pos + 1), (i)=>{scene.my.tempArray = scene.my.tempArray ++ [array(i + (arrayLength - pos))]}); geval("" + arrayString + "= scene.my.tempArray") }
You may notice the ridiculous amount of arguments, but they are all necessary, due to restrictions in thyme
The arguments are pos, value, array, arrayLength and arrayString.
pos: The value you want to change, e.g. array = [0,1,2] if i wanted to change the 1, the pos would be 1, because it starts from 0, counting upwards. so the 0 is array(0) and so on. Like if you were reading the array with Thyme.
value: What you want to change the value at pos to. Fairly self explanatory.
array: Even more self explanatory. e.g. scene.my.exampleArray
arrayLength: This is confusing; it is the length of the array, but starting from 1 for the first value. So the array [1,0,0, 1,0,1] would have a length of 6.
arrayString: This seems unnecessary, but is essential. It is just the same as what you put in as the array argument, just as a string. So , if you put scene.my.arrayExample as the argument for array, then this would be "scene.my.arrayExample". Or, any other array you want to read the end value into, but remember the quotes.
I realise i don't need the array argument, with the arrayString argument, but i am to lazy to fix this
Hope this helps!
EDIT:
Oops, forgot an example:
- Code: Select all
>scene.my.array = [1,2,3,56,5,6,7]
//want to change the 56 to 4
>scene.my.arrayEdit(3, 4, scene.my.array, 7, "scene.my.array")
[1,2,3,4,5,6,7]
>scene.my.array
[1,2,3,4,5,6,7]
-

bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
Re: Editing Array Values: Solution
I made a function for this problem two years ago. With this one you can also change higher dimensional array values.
array edit function
For example (1-dimensional change):
For example (3-dimensional change):
And you don't need to know the length of the array because the function detects it automatically.
array edit function
For example (1-dimensional change):
- Code: Select all
scene.my.array := [1,2,3,4,5];
scene.my.arrayChange(scene.my.array, 1, 253)
[1,253,3,4,5]
For example (3-dimensional change):
- Code: Select all
scene.my.array := [1,2,4,5];
scene.my.arrayChange(scene.my.array, 1, [1,2,3])
[1,[1,2,3],3,4,5]
And you don't need to know the length of the array because the function detects it automatically.

-

KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Editing Array Values: Solution
Wow... I didn't even know that existed... or was possible...
You are a master math-function-Thymer who should, in my opinion, add scene.my.arrayLength and scene.my.arrayChange to the Some Extra Math Functions thread. Some of the most useful functions I know of...
After re-reading that topic, I must say this: Congratulations on the great work once again.
You are a master math-function-Thymer who should, in my opinion, add scene.my.arrayLength and scene.my.arrayChange to the Some Extra Math Functions thread. Some of the most useful functions I know of...
After re-reading that topic, I must say this: Congratulations on the great work once again.
Matthias Wandel is epic, in my humble opinion.
I love my brain...
ARE YA HAPPY NOW?????
Thymechanic/Phundamentalist
Recently, I discovered something a lot of you probably already knew: Minecraft is awesome.
Due to this, I may not be as active as usual for a while.
I love my brain...
TC42 wrote:Also, your sig is too big, please change it.
ARE YA HAPPY NOW?????
Thymechanic/Phundamentalist
Recently, I discovered something a lot of you probably already knew: Minecraft is awesome.
Due to this, I may not be as active as usual for a while.
-

Someone Else - Posts: 1147
- Joined: Sun Nov 21, 2010 10:53 pm
- Location: The Milky Way Galaxy
Re: Editing Array Values: Solution
@KarateBot: Your function is so much neater than mine 
-

bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
4 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 7 guests




