Download MetaTrader 5
To add comments, please log in or register
Do you want to write a technical indicator? Find solution in MQL5 Reference!
Ernie Gunning
1147
Ernie Gunning 2014.11.23 10:04 

Hi Guys,

 I cant start this thread saying I'm a noob programmer even though I really want to because this is a silly question :) and it makes me feel like a noob lol. Coming from the C# world this is simple.

 I have the need to have a string array (string MyString[]) and at a certain point I want to remove a item or element. I can re-size the array, i can full the array and free it... but how to delete item in the middle for example? is this possible? 

honest_knave
968
honest_knave 2014.11.23 10:35  
If the order of the array doesn't matter, copy the last index to the index you want removed then resize the whole array to be one smaller.
Ernie Gunning
1147
Ernie Gunning 2014.11.23 10:39  

Sorry guys I don't know why this always happens but ArrayCopy popped up in my mind after posting. I don't know if this is the actual solution for MQL4 but in my mind this is a workaround. I used Array copy to remove the index. Here is the code and please post a better solution if any...

 The RemoveIndexFromArray Function does the trick....

Thanks 

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string MyArray[];
   ArrayResize(MyArray,10,10);
   int arraySize = ArraySize(MyArray);
   for(int i=0;i<arraySize;i++)
     {
      MyArray[i] = i+"";
     }
   //Now we have array filled from 0-9. we want to delete index 5
   int IndexToDelete =5;
   //This is were I am. A array with data.
   RemoveIndexFromArray(MyArray,5);
  }
//+------------------------------------------------------------------+
void RemoveIndexFromArray(string &MyArray[],int index)
{
   string TempArray[];
   //Lets copy index 0-4 as the input index is to remove index 5
   ArrayCopy(TempArray,MyArray,0,0,index);
   //Now Copy index 6-9, start from 6  as the input index is to remove index 5
   ArrayCopy(TempArray,MyArray,index,(index+1));
   
   //copy Array back
   ArrayFree(MyArray);
   ArrayCopy(MyArray,TempArray,0,0);
}
Ernie Gunning
1147
Ernie Gunning 2014.11.23 10:43  
honest_knave:
If the order of the array doesn't matter, copy the last index to the index you want removed then resize the whole array to be one smaller.

Thanks honest this is what I ended up doing, it is a sleph though. Can you think of a better solution?

Thanks for your valuable feedback! 

Keith Watford
Moderator
7953
Keith Watford 2014.11.23 11:08  

Maybe this will help you

  string Text="";
  int x;
  string MyArray[];
  ArrayResize(MyArray,10);
  for(x=0;x<10;x++)
     {
     MyArray[x]=(string)x;
     }
  for(x=0;x<10;x++)
     {
     Text+=MyArray[x]+" ";
     }
  Print(Text);
  
  //I want to remove MyArray[3] and MyArray[7] which is "3" and "7"
  
  int offset=0;
  for(x=0;x<10;x++)
     {
     if(x==3 || x==7)
        offset++;
     else
        MyArray[x-offset]=MyArray[x];
     }
  ArrayResize(MyArray,8);
  Text="";
  for(x=0;x<8;x++)
     {
     Text+=MyArray[x]+" ";
     }
  Print(Text);
honest_knave
968
honest_knave 2014.11.23 11:25  
ToolMaker:

Thanks honest this is what I ended up doing, it is a sleph though. Can you think of a better solution?

Thanks for your valuable feedback! 

Within the limitations of MQL4, it's the way I do it. There isn't much to it, only a few lines of code.

whroeder1
13623
whroeder1 2014.11.23 12:17  
ToolMaker: Can you think of a better solution?
//I want to remove MyArray[3] and MyArray[7] which is "3" and "7"

EraseOrdered(MyArray,3);
EraseOrdered(MyArray,6); // "7" is now in position 6

// or 
Erase(MyArray,3);
Erase(MyArray,7);
Not compiled, not tested.
  1. Elements need to stay in order
    template <typename T> void EraseOrdered(T& A[], int iPos){
       for(int iLast = ArraySize(A) - 1; iPos < iLast; ++iPos) 
          A[iPos] = A[iPos + 1];
       ArrayResize(A, iLast);
    }
    
  2. No
    template <typename T> 
    void Erase(T& A[], int iPos){
       int iLast = ArraySize(A) - 1;
       A[iPos] = A[iLast];
       ArrayResize(A, iLast);
    }
    
Not compiled, not tested.
honest_knave
968
honest_knave 2014.11.23 15:42  

You'll probably need an ampersand in there to avoid a compiler warning.

whroeder1
13623
whroeder1 2014.11.23 18:25  
honest_knave: You'll probably need an ampersand in there to avoid a compiler warning.
Yep, thus the:
Not compiled, not tested.
Ernie Gunning
1147
Ernie Gunning 2014.11.24 05:45  
WHRoeder:
Not compiled, not tested.
  1. Elements need to stay in order
  2. No
Not compiled, not tested.

WOW!!! Beautiful CODE thanks!!! didn't know MQL4 even had generics!! thanks allot WHReader!

 I was looking for some one liner from the MQL code but seems we need to write our own removal function. I will apply this generics to my removal and addition function.

 Thanks WHReader, It is code like this that excites me...something new!! 

you rock! 

whroeder1
13623
whroeder1 2014.11.24 16:08  
ToolMaker: didn't know MQL4 even had generics!!
  1. Amazing what you learn when you read the documentation.
  2. Functions only, no template classes. Function templates - MQL4 Documentation
/
To add comments, please log in or register