Exclude/remove a specific element of an Array with string data

filipe197
50
filipe197  

Hello,
I would like to code a simple situation in theory, but i am kind of struggling with that. Imagine I have an array with string data like this:


FirstArray [8] = {"A","B","C","D","E","F","G","H"}


I am having the need to remove one specific position which can be any of the 8 elements, and then resize the array to 7 elements. For instance, let´s say I want to remove index 3, which represents string "C" of the array above. Succeeding on this would mean I end up having my final array like this:


FinalArray [7] = {"A","B","D","E","F","G","H"}


Can you help me on this?
Thanks you a lot in advance.

William Roeder
23446
William Roeder  
filipe197: I would like to code a simple situation in theory, but i am kind of struggling with that.

Move higher elements down to the deleted one by one and resize the array.

Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.

          No free help 2017.04.21

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum 2018.05.12

Lorentzos Roussos
24310
Lorentzos Roussos  
filipe197:

Hello,
I would like to code a simple situation in theory, but i am kind of struggling with that. Imagine I have an array with string data like this:


FirstArray [8] = {"A","B","C","D","E","F","G","H"}


I am having the need to remove one specific position which can be any of the 8 elements, and then resize the array to 7 elements. For instance, let´s say I want to remove index 3, which represents string "C" of the array above. Succeeding on this would mean I end up having my final array like this:


FinalArray [7] = {"A","B","D","E","F","G","H"}


Can you help me on this?
Thanks you a lot in advance.

Hello.

There are 2 approaches . 

In the first one ,you dont care about the order of the array elements ,so you pull the last element ,place it in the position of the element you want to delete ,and reduce the array.(overwrite)

First Method : 

void FirstMethod(string &reference_to_array[],int element_to_remove)
{
//get current size
int current_size=ArraySize(reference_to_array);
//if size okay and element okay 
if(current_size>0&&element_to_remove<=current_size&&element_to_remove>0)
{
//overwrite 
  reference_to_array[element_to_remove-1]=reference_to_array[current_size-1];
//shrink
  if(current_size-1==0){ArrayFree(reference_to_array);}
  if(current_size-1>0){ArrayResize(reference_to_array,current_size-1,0);}
}
//if size okay and element okay ends here
}

to use this call :

FirstMethod(FirstArray,3);//we pass the reference to the array ,and the element to be removed (the element in the array will actually be -1 ,so [2] will be removed

In the second approach , you care about the order of the elements so you pull everything below the removed element up

Second Method : 

void SecondMethod(string &reference_to_array[],int element_to_remove)
{
//get current size
int current_size=ArraySize(reference_to_array);
//if size okay and element okay 
if(current_size>0&&element_to_remove<=current_size&&element_to_remove>0)
{
//loop from element to remove -1 which is the location in the array to total elements -2 because the last actual element has nothing to pull
  for(int p=(element_to_remove-1);p<=(current_size-2);p++)
     {
     reference_to_array[p]=reference_to_array[p+1];//pull up the element below
     }
//shrink
  if(current_size-1==0){ArrayFree(reference_to_array);}
  if(current_size-1>0){ArrayResize(reference_to_array,current_size-1,0);}
}
//if size okay and element okay ends here
}

to use this call : 

SecondMethod(FirstArray,3);
filipe197
50
filipe197  
Lorentzos Roussos:

Hello.

There are 2 approaches . 

In the first one ,you dont care about the order of the array elements ,so you pull the last element ,place it in the position of the element you want to delete ,and reduce the array.(overwrite)

First Method : 

to use this call :

In the second approach , you care about the order of the elements so you pull everything below the removed element up

Second Method : 

to use this call : 

Many thanks Lorentzo ;)
Enrique Dangeroux
735
Enrique Dangeroux  

There is no need for any of the above.

https://www.mql5.com/en/docs/array/arrayremove