Off Topic > Off Topic
Programming Megathread
Waru:
so in gml is there a way to cascade arrays?
like
--- Code: ---array[0] = 5
array[1] = 6
array[2] = 7
array[3] = 8
array[4] = 9
array[5] = 10
array[6] = 11
--- End code ---
and you do a command so all the arrays are down one?
like
--- Code: ---array[0] = x (entered later)
array[1] = 5
array[2] = 6
array[3] = 7
array[4] = 8
array[5] = 9
array[6] = 10
array[7] = 11 (or deleted to keep only 7)
--- End code ---
i know i can do it with a long string of array[1] = array[0] going backwards from whatever max I want, but just wondering if there's a better way.
Foxscotch:
I'm not sure what you're trying to do. you want to move every item in an array down a few indices? what happens to the items that would be below index 0?
I don't understand the second example at all. what do you mean "entered later" and "deleted to keep only 7"?
Waru:
--- Quote from: Foxscotch on March 09, 2016, 05:24:52 PM ---I'm not sure what you're trying to do. you want to move every item in an array down a few indices? what happens to the items that would be below index 0?
I don't understand the second example at all. what do you mean "entered later" and "deleted to keep only 7"?
--- End quote ---
i want to move everything up?
I have 7 indexes at the start, i was wondering if there was a way to move all indexes from x to x + 1, and then fill in the index of 0 with something, the last part about "deleted to keep only 7" meant having array[7] cleared so the number of indexes is always 7. (Which can be solved by just not writing to [7]
i know it's completely possible to do something like
--- Code: ---array[0] = 5
array[1] = 6
array[2] = 7
array[3] = 8
array[4] = 9
array[5] = 10
array[6] = 11
--- End code ---
then
--- Code: ---array[0] = 4
array[1] = array[0]
array[2] = array[1]
array[3] = array[2]
array[4] = array[3]
array[5] = array[4]
array[6] = array[5]
--- End code ---
i was just wondering if there was a better way to do it
Kingdaro:
Something like this?
first = array[0] + 1;
for (i = 0; i < len(array); i++) {
array[i] = first + i;
}
I'm not sure what it'd be exactly in GML, but it's what you're most likely looking to do. That, or a push/pop function for arrays, if GML has it.
Foxscotch:
oh, the first list was the starting list, and the second was the goal
lol, that explains a lot. I was thinking you were saying like, array[0] = array[5] or something
(the confusion came from you using "=" instead of "==")
anyway idk GML, but this would work in javascript
function cascade(oldArray, x) {
var array = [];
for (var i = 0; i < oldArray.length; i++) {
array[i + x] = oldArray[i];
}
return array;
}
the x argument is for the number of places you want to move everything up. to get what you want, you'd just use 1
< x = [1, 2, 3]
< y = ['hey', 'hi', 'hello']
< z = [3, 2, 1]
< cascade(x, 1)
> [undefined, 1, 2, 3]
< cascade(y, 2)
> [undefined, undefined, "hey", "hi", "hello"]
< cascade(z, 3)
> [undefined, undefined, undefined, 3, 2, 1]