How can I change directly an element of an array?

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

How can I change directly an element of an array?

Postby lololoer16_17 » Mon Mar 03, 2014 6:13 pm

I want to make a function to change an element of an array directly this could be very useful to long list, such as surfaces, instead of typing all the array, typing the element(byIndex).
I maded this code to help people to insert an element between 2 or more in an array (with help of kilinich's xfor code).
Code: Select all
scene.my.insertElement = (a, x, eL)=>{
    xFor = (n1, n2, code)=>{
        n2 > n1 ? {
            m := (n1 + n2) / 2;
            xFor(n1, m, code);
            xFor(m + 1, n2, code)
        } : {code(n1)}
    };
    n = (z)=>{
        string.length(z) - 1
    };
    Ge1 := [];
    Ge2 := [];
     xFor(0, eL, (i)=>{
        Ge1 = Ge1 ++ [a(i)]
    });
     xFor(eL + 1, n(a), (i)=>{
        Ge2 = Ge2 ++ [a(i)]
    });
    Ge1 ++ [x] ++ Ge2
}


I think that using this function as point reference, and changing something on it (wich I don't know how to), will work.
The function would be like: Array = scene.my.function(Array,index,element).
lololoer16_17
 
Posts: 18
Joined: Tue Jan 21, 2014 8:55 pm
Location: Spain (South)

Re: How can I change directly an element of an array?

Postby Xray » Mon Mar 03, 2014 7:49 pm

If this is used to insert an element into an existing array, then the entire array will become larger. Is that correct?
For example, if I have array: scene.my.array[3,8,2,4] and if I use your function to insert a 7 into element #1, then the result will be
scene.my.array[3,7,8,2,4]. Is that correct?

Another question: You state that this function is used to "change directly an array element". That is different from inserting a new element. So, which is correct, insert or change? :?
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: How can I change directly an element of an array?

Postby lololoer16_17 » Mon Mar 03, 2014 8:39 pm

Xray, forget it , I did not explained very good, just you have to know that I'm asking for a fnction that changes
directly an elemwnt of an array.
lololoer16_17
 
Posts: 18
Joined: Tue Jan 21, 2014 8:55 pm
Location: Spain (South)

Re: How can I change directly an element of an array?

Postby Xray » Mon Mar 03, 2014 10:45 pm

This is what I use for writing to a single array element:

scene.my.xFor := (n1, n2, code) => {
n2 > n1 ? {
m := (n1 + n2) / 2;
scene.my.xFor(n1, m, code);
scene.my.xFor(m + 1, n2, code)
} : {code(n1)}
};

scene.my.WriteArray := (array, n, new) => { result := []; scene.my.xFor(0, string.length(array)-1, (i)=>{ i == n ? { result = result ++ [new]} : {result = result ++ [array(i)]} }) }

Enter the above code into the console.

Then to use the function, this is the script: array=scene.my.writearray(array,n,value)

This has worked very well for me, and I have used it in every scene that I made that has arrays. You are welcome to use it too!
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: How can I change directly an element of an array?

Postby lololoer16_17 » Tue Mar 04, 2014 7:59 pm

Xray - Wow! Have you maded that function by yourself? if so, Good job and thankyou! :clap: :clap:
lololoer16_17
 
Posts: 18
Joined: Tue Jan 21, 2014 8:55 pm
Location: Spain (South)

Re: How can I change directly an element of an array?

Postby Xray » Tue Mar 04, 2014 9:11 pm

No I did not invent that code. It was invented by user KarateBrot, and he gave me permission to use it in my scenes. I made a few minor changes to it, but it basically works the same as his original script. It is my favorite script because it is so very useful! :D
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: How can I change directly an element of an array?

Postby Kilinich » Wed Mar 05, 2014 7:40 am

Xray wrote:No I did not invent that code. It was invented by user KarateBrot, and he gave me permission to use it in my scenes. I made a few minor changes to it, but it basically works the same as his original script. It is my favorite script because it is so very useful! :D


xFor is mine :ugeek: but it's not necessary to use here)
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: How can I change directly an element of an array?

Postby Xray » Wed Mar 05, 2014 10:01 am

Kilinich designed "scene.my.xfor": Thyme 2.0
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 3 guests