The fastes shift all element on Array to left (shift one position inside array)

Back to topics list To post a new topic, please log in or register
avatar
193
endy5 2013.09.22 13:06
 

Hi guys,

I have array, 100 elements and I need shift all elements left, index 99 bye bye :-) from index 98 to index 99, 97->98, 2->3 .... index 0 = zero

What is the fastes solution for it? Exist any special mql function?

Or basic solution ...:

int count=ArraySize(array);
for(int i=count-2; i>1; i--) array[i]=array[i-1];
array[0]=0;
 
avatar
12988
WHRoeder 2013.09.22 15:08 #
 
From my code
bool     ResizeBuffer(double& buffer[], int size){ // buffer can be 1D or 2D
   if(ArrayRange(buffer,0) != size){      // ArrayRange allows 1D or 2D arrays
      ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
      if( !MyArrayResize(buffer, size, "ResizeBuffer") )        return(false);
      ArraySetAsSeries(buffer, true);
   }
   return(true);
}
bool     MyArrayResize(double& arr[], int nRows, string msg){
   if( ArrayResize(arr, nRows) >= nRows)  return(true);
   DisableTrading( "ArrayResize("+nRows+") Failed: "+GetLastError() +" "+msg);
   return(false);
}
////////////////
double Typical[]; datetime TypicalCounted=0;
int start(){
  ResizeBuffer(Typical, Bars); 
  for(int iBar=iBarShift(NULL,0,TypicalCounted); iBar >=0; iBar--) 
     Typical[iBar] = (High[iBar]+Low[iBar]+Close[iBar])/3;
  TypicalCounted = TimeCurrent();

  Print("3 bars ago typical price was "+PriceToStr(Typical[3]));
No shifting required.
avatar
352
GreenMoney 2013.09.22 16:02 #
 
endy5:

I have array, 100 elements and I need shift all elements left, index 99 bye bye :-) from index 98 to index 99, 97->98, 2->3 .... index 0 = zero

What is the fastes solution for it? Exist any special mql function?

I don't know about the fastest, but you could simply use ArrayCopy() to shift the elements:

int count = ArraySize(array);
if (ArrayCopy(array, array, 1, 0, count - 1) == count - 1)
   array[0] = 0;


 
avatar
18
Roel13 2015.04.01 21:18 #
 
Old thread, I know, but this may help someone. It seems like the original code listed has "-2" and that should read "-1"  and it has ">1" which should read ">=1" - correct?
Back to topics list  

To add comments, please log in or register