How can I change directly an element of an array?
8 posts • Page 1 of 1
How can I change directly an element of an array?
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).
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).
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?
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?
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?
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: How can I change directly an element of an array?
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.
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?
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!
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!
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: How can I change directly an element of an array?
Xray - Wow! Have you maded that function by yourself? if so, Good job and thankyou!

- 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?
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! 
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: How can I change directly an element of an array?
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!
xFor is mine
Dream of Algodoo as game development engine...
-

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?
Kilinich designed "scene.my.xfor": Thyme 2.0
-

Xray - Posts: 501
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
8 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 3 guests



