How can i splice current index in a foreach? - arrays

I have this foreach loop to check for collision and i want platform(movieclip) to be removed in case of collision. So far i've come up with this:
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y)
{
mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
jump();
mcContent.removeChild(platformCloud);
//platformsCloud.splice(platformCloud);
}
What this is doing is, removing the movieclip (ok so far so good) but without the splice, when the loop runs again through the array it is still there. So with the splice that is commented out there's 1 little problem, it removes all the movieclips from the array, apprently.
How can i splice only the current index that is being checked?

.splice() accepts a start index and an amount of items to remove, not the object you want to remove from the array.
Parameters
startIndex:int — An integer that specifies the index of the element in the array where the insertion or deletion begins. You can use a negative integer to specify a position relative to the end of the array (for example, -1 is the last element of the array).
deleteCount:uint — An integer that specifies the number of elements to be deleted. This number includes the element specified in the startIndex parameter. If you do not specify a value for the deleteCount parameter, the method deletes all of the values from the startIndex element to the last element in the array. If the value is 0, no elements are deleted.
You want to do this:
var index:int = platformsCloud.indexOf(platformCloud);
platformsCloud.splice(index, 1);

Why not just create a new array of the items to keep? Use Array.push to add new items. This may actually be more efficient than modifying the existing array. It also doesn't require keeping track of indices (which are required to use Array.splice).
Example code:
var keptPlatforms = [];
// do stuff
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y)
{
mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
jump();
mcContent.removeChild(platformCloud);
} else {
keptPlatforms.push(platformCloud);
}
// later, after this cycle, use the new Array
platformClouds = keptPlatforms;
Now, the reason platformsCloud.splice(platformCloud) removes all items is because the first argument is coerced to an integer so it is equivalent to platformsCloud.splice(0) which says "remove the 0th-indexed item to the end of the array". And, this does indeed clear the array.
To use Array.splice, you'd have to do something like:
// inside a loop this approach may lead to O(n^2) performance
var i = platformClouds.indexOf(platformCloud);
if (i >= 0) {
platformClouds.splice(i, 1); // remove 1 item at the i'th index
}

Related

Efficient way to filter an array based on element index

I have an array of arrays. So each element of my first array contains a comma separated list of values. If I use the split function, I can get an array from this comma separated list. What I need to do is filter out this second array based on element position. For example only keep columns one, three, five and nine.
One way to do this is loop thru my first array, for each element do a split on the element to get my second array. Then loop thru this second array, increment a counter to track the current element index. If the counter is equal to one of the columns I want to keep, then concat the element to a string variable.
This is very inefficient and takes forever to run on large arrays. Does anyone have any ideas on a better way to do this? I hope I explained this clearly.
There are some built in array actions like “Filter” and “Join” as you mention, but for something this specific I imagine you’ll need to call some code (e.g. azure function) to quickly do manipulation and return result
For the first loop I don't know of any alternate but for second loop, Instead of looping through the second array,you can simply access the elements with index that you require.
Assuming the size is not an issue.
string[] Arr1 = new string[] { "0_zero,0_One,0_Two,0_Three,0_Four,0_Five,0_six,0_seven,0_eight,0_nine",
"1_zero,1_One,1_Two,1_Three,1_Four,1_Five,1_six,1_seven,1_eight,1_nine" };
string myString = string.Empty;
foreach(var a in Arr1)
{
var sp = a.Split(',');
myString= string.Concat(myString, sp[0], sp[3], sp[5], sp[9]);
}
Console.WriteLine(myString); //gives "0_One0_Three0_Five0_nine1_One1_Three1_Five1_nine"
In case we're not sure of length of each string, we can use if else ladder with decreasing order from maximum index that we want to use like so
foreach(var a in Arr1)
{
var sp = a.Split(',');
int len = sp.Length;
if (len >= 10) myString= string.Concat(myString, sp[1], sp[3], sp[5], sp[9]);
else if (len >= 6) myString = string.Concat(myString, sp[1], sp[3], sp[5]);
else if (len >= 4) myString = string.Concat(myString, sp[1], sp[3]);
else if (len >= 2) myString = string.Concat(myString, sp[1]);
}
So that we don't face IndexOutofBoundsException

Is it safe to iterate an array while modifying it?

I know you shouldn't, I kind of know why. But I mean I don't understand my own code once I am trying really to think what's going on.
So I have an array with bunch of objects. I am iterating over it and once I find an object with specific type, I remove it from the array, and add another object into the array. So something like this:
var arr = parent.allchildren() //getting all the children in array
for ele in arr{
if(ele==somethingHere){
parent.remove(ele)
parent.add(new ele) //add new child into child array
}
}
If I have an array of 1,2,3,4,5, and I remove 3 and add a 6 while iterating, the actual array would be 1,2,4,5,6 but the array I am iterating would still be 1,2,3,4,5.
Which I think it would be fine, because at the end I still get what I want, which removed the element and added the element I need. However modifying the list while iterating it is bad and you shouldn't do that, but for my case I think it does what I need. What could be the potential issue in my case that I can't see?
One thing you may want to think about doing is making all of the changes at the end of the iteration. Instead of making the changes one by one, record the changes you want to make while iterating, and then actually make those changes once your loop is finished.
For example, you could make an array of elements to remove, and an array of elements to add.
//Our array where we record what we want to add
var elementsToAdd = [Any]()
//Our array of what elements we want to remove. We record the index at
//which we want to remove the element from the array
var indexesToRemoveAt = [Int]()
//Getting all the children in array
var arr = parent.allchildren()
//Enumerating an array allows us to access the index at which that
//element occurs. For example, the first element's index would be 0,
//the second element's index would be 1, the third would be 2, and so
//on
for (index,ele) in arr.enumerated() {
if(ele == somethingHere) {
indexesToRemoveAt.append(index)
elementsToAdd.append(newEle)
}
}
//Now that we have recorded the changes we want to make, we could make
//all of the changes at once
arr.remove(at: indexesToRemoveAt)
arr.append(contentsOf: elementsToAdd)
Note that removing array elements at multiple indexes would require the following extension to Array. If you wanted to avoid creating this extension, you could always just loop through the array of indexes and tell the array to remove at each individual index. All this extension function is really doing is looping through the indexes, and removing the array element at said index.
Array extension to remove elements at multiple indexes:
extension Array {
//Allows us to remove at multiple indexes instead of just one
mutating func remove(at indexes: [Int]) {
for index in indexes.sorted(by: >) {
if index <= count-1 {
remove(at: index)
}
}
}
}
I just tested in a playground with the following code:
var arr = ["hi", "bye", "guy", "fry", "sky"]
for a in arr {
if arr.count >= 3 {
arr.remove(at: 2)
}
print(a)
}
print(arr)
This prints:
hi
bye
guy
fry
sky
["hi", "bye"]
So it looks like when you use a for-in loop in Swift, the array is copied and changes you make to it will not affect the array you are iterating over. To answer your question, as long as you understand that this is the behavior, there's nothing wrong with doing this.

Last cell in array that modified

Is there any way to find out which cell in an array is the last cell that modified? (e.g. change its value)
In any compile-based languages.
For example assume we defined array1 with n cells. Now value of array1[2] changed to 1, after that value of array1[7] will change to 1, I want a solution to find out the 7th cell as last cell that modified.
Just don't access the array directly, write to it in a function/method and keep track as it changes. In C# there are properties/indexers which wrap the call X = 2 in a method for you.
Neither of these seem like particularly great solutions, but they will both accomplish what you want in C++:
int indexToChange, lastIndexChanged;
int array1 [10];
indexToChange = 2;
array1[indexToChange] = 1;
lastIndexChanged = indexToChange;
indexToChange = 7;
array1[indexToChange] = 1;
lastIndexChanged = indexToChange;
Then at any point array1[lastIndexChanged] would be the most recently updated. But this is annoying since it requires 3 commands for every array update. Instead, you could wrap it into a function:
void changeArray(int array[], int length, int indexToChange, int newVal){
array[indexToChange] = newVal;
lastIndexChanged=indexToChange;
}
Which would then require all your array updates to look like this (assuming lastIndexChanged was declared globally and array1 has size 10):
changeArray(array1, 10, 2, 1);
changeArray(array1, 10, 7, 1);
Then, to update the most recently changed element to 0:
changeArray(array1, 10, lastIndexChanged, 0);
However, both of these examples will only work for a single array in a program, which does not seem particularly useful to me.
Only other idea I have involves creating an array of tuples (or something similar) and using the 2nd element of the tuple as one of the following:
A bool flag indicating if the element was the last one changed
An int flag indicating the "age" of the element
However, both of those methods require accessing every element of the array for every single array update.
For (1), with each update you would have to make sure to find the previously last updated element and set it's flag to false as you set the flag of the element you were updating to true.
For (2), with each array update you would increment the "age" flag of every element, but set the "age" of the element you updated to 0. This has the advantage of enabling you to also find the nth last updated element.
In either case, if your original array is an array of ints, you may be able to implement this with a 2-dimensional array with two rows. So, if array1 is size n, you would have int array1[2][n], giving you an array like this (for n=4, assuming all cells initialized to 0):
[0][0][0][0]
[0][0][0][0]
where you would use the top row for your values and your bottom row for flags. Thus, an array update would look something like this:
For "bool" flags (just using 0 and 1 to simulate a bool value):
array1[0][2] = 1; //set value of element 2 to 1
array1[1][getLastUpdated()]=false; //resetting the previous "last updated" flag
array1[1][2] = true;
For "age" flags:
array1[0][2] = 1; //set value of element 2 to 1
for (int i=0; i<array1Length; i++){
array1[1][i]++; //increment ages of every element
}
array1[1][2] = 0; //reset age of the element you just updated
Then, to find the most recently updated element, you would search for array[1][n] to be true or 0, respectively.

AS-3 - How to get min/max INDEX of Array

This is NOT about min/max values!
I use Array with all kinds of indices like 0, 1, 2, 1000, -1, -100. Yes, negative indices (I like this feature and I'm thankful for it.). The problem is I need them to be negative otherwise I would have to move all items. Now I wonder how can I retrieve the min index like -100. Most elements between min and max index would be undefined.
var a: Array = new Array();
a[-100] = "hello";
a.forEach(...); // starts with 0
Is there a simple way to get min index ?
No, there is no simple way to get the minimum index of the negative keys. You would have to loop through all the keys, convert them to numbers, and get the lowest.
The reason for that is that you are using the array both as an array and as an object. The array only has items with positive indexes, when you assign anything to it using a negative index, it's instead added as a property to the array object, not as an item in the array.
The forEach method loops through the items in the array, so it will only access the positive indexes. The data that you added using negative indexes are not array items, they are properties of the array object. So, it's not a bug that the forEach method starts from index 0.
If you want to loop through everything that you stored in the array, you can loop through its properties, then you will get both the object properties and array items:
for (i in a) {
// Here i is the key, and a[i] is the value
}
It's true that you can have negative indices in AS3 Arrays. The problem (and it's quite a serious one) is that none of the Array methods recognise the negative indices - they only recognise whole numbers.
For example:
var my_array:Array = new Array();
var my_array[-100] = "hello";
trace(my_array[-100]) // hello
trace(my_array.length) // 0
trace(my_array) // [empty]
So standard iteration through the array can only be done with absolute references to the index. Or by treating the array as an Object when looping:
var lowest:int = -100000 /* some deep number */
for(var i in a){
if(int(i) < lowest){
lowest = int(i);
}
}

Arrays not filling properly AS3

I wrote an array, where, when a hittest occurs, a number should be pushed into the array. However, when I use myArray.push, it enters the number, and when i call myArray.push again, instead of entering the number again and having 2 numbers in my array, it just enters the number in the same spot. So, if i use trace(myArray.length), no matter how many times it has pushed, it keeps saying I have 1 number stored.
This is my code:
if (hitTestObject(target.hit)) {
//pushes a number into an array
myArray.push();
//checks array length
if (myArray.length == 3) {
//do stuff
}
}
I've tried numbers, strings, and Math.random() in the parameters for myArray.push(), but I always get the same result of only having 1 item in my array.
For reference, this is how I wrote my variable for it:
public var myArray:Array = new Array();
You have to push something into the array. Try:
myArray.push(myArray.length);
However, it looks like you'd be better off just incrementing a number rather than creating an array. An array is a container of objects to be later referenced. So, unless you need to track that the third object in the array is in fact '2'...the array is useless. Try creating a number and increment the variable each time. Like this:
// Declared outside of the function
var integer:int = 0;
//
if (hitTestObject(target.hit)) {
integer = integer + 1;
// likewise you could use ++integer or integer++;
if (integer == 3) {
//do stuff
}
}
Based on your code, you are not actually pushing anything into the array.
The correct syntax is :
myArray.push(something);

Resources