change single items of an array
Hi,
i would like to know how i change single items of an array.
Let's say if have an array := [a,b,c] and i want to change item 2 [b] to f. The final array will look like [a,f,c].
How can i do that?
Kind regards
Christoph
8 replies
-
Maybe...
// are comments
var p := 2; //Position to change
var t := "f"; //change it To
var x := ["a", "b", "c", "d", "e"]; //data
var a := slice(x, 0, p);//data before position
var b := slice(x, p, p + 1);//the position data for clarity
var c := slice(x, p + 1, cnt(x));//data after the position
x := split(concat(a) + ", " + t + ", " + concat(c), ", ");//put it back together and make it an array — watch the spaces with the commas
-
Hallo JCSchell,
thanks for your response. I was aware of this kind of a workaround. But it is waaaay to complicated and needs to much effort for such an easy operation. There must be an easier way. Something similar like:
item (array,3) := f
Kind regards
-
Agreed
-
But is there an easier way? @NinoxTeam
-
let myArray := ["a", "b", "c"];
myArray := [item(myArray, 0), "f", item(myArray, 2)];
alert(text(myArray))It's not great, but it does preserve the values you don't want to change.
-
that's not perfect but should do the job. thx :)
-
Hi
How about something like:
let array1 := ["a", "b", "c"]; let changeItem := 2; for i in array1 do if index(array1, i) = changeItem - 1 then "f" else i end end
the result is
a,f,c
You can change the value in line 2 to 1,2, or 3. Or if your array is bigger to any number that makes sense in the array.
We use the handy index() command to get us the space in the index the value occupies. In Ninox all arrays start with 0, so that is why in line 4 we have to subtract one from the changeItem value. I left it this way so it is more human friendly as we think count the values in an array starting with 1.
Content aside
- 6 days agoLast active
- 8Replies
- 2196Views
-
2
Following