Metatrader4, Trading

MQL4: How to remove elements from an Array

This post has some MQL4 code which will help you remove elements from an array by their Index or Values.

It uses templates so it can be used for any type of array.
Note that this is for 1-Dimensional arrays only.

Remove Array Code Snippet:

template <typename T> void RemoveValueFromArray(T& A[], T value){
   bool isShiftOn = false;
   for(int i=0; i < ArraySize(A) - 1; i++) {
      if(A[i] == value) {
         isShiftOn = true;
      }
      if(isShiftOn == true) {
         A[i] = A[i + 1];
      }
   }
   ArrayResize(A, ArraySize(A) - 1);
}
template <typename T> void RemoveIndexFromArray(T& A[], int iPos){
   int iLast;
   for(iLast = ArraySize(A) - 1; iPos < iLast; ++iPos) 
      A[iPos] = A[iPos + 1];
   ArrayResize(A, iLast);
}

If you have any more such useful snippets for Arrays, please share with us in the comments.

Thanks.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments