Editing Array Values: Solution

About advanced scenes, and the Thyme scripting language used in Algodoo.

Editing Array Values: Solution

Postby bozbez » Mon Jul 11, 2011 8:32 pm

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:
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 :D

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]
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm

Re: Editing Array Values: Solution

Postby KarateBrot » Fri Jul 22, 2011 7:13 am

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):
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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Editing Array Values: Solution

Postby Someone Else » Sat Jul 23, 2011 5:48 am

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.
Matthias Wandel is epic, in my humble opinion.
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.
User avatar
Someone Else
 
Posts: 1147
Joined: Sun Nov 21, 2010 10:53 pm
Location: The Milky Way Galaxy

Re: Editing Array Values: Solution

Postby bozbez » Sat Jul 23, 2011 10:56 am

@KarateBot: Your function is so much neater than mine :)
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 7 guests