Storing elements from one array to another based on specified conditions

 

Good day,


I am trying to transfer elements from one array to another but only if the values meet specific criteria. (E.g. If the value is !=0).


Can someone please point me in a direction of how to approach this. Below are some thought I had but I have no clue. Please keep in mind that I am NEW TO PROGRAMMING!!

//This is just an extract of the code

double ZigZag = iCustom(NULL,PERIOD_H1,"Examples\\ZigZag",InpDepth,InpDeviation,InpBackstep);

double ZigZag_Array [], ZigZag_Array2[];


ArraySetAsSeries(ZigZag_Array,true);
CopyBuffer(ZigZag,0,0,count,ZigZag_Array);

ArraySetAsSeries(ZigZag_Array2,true);


//1st Thought
  for(int k=0,j=0;j<count;j++)
     {
      if(ZigZag_Array[j]!=PLOT_EMPTY_VALUE && ZigZag_Array[j]!=0.0){
           ZigZag_Array2[k]=ZigZag_Array[j];
            k++;
         }
         else
         Print("Error in copying Arrays" + GetLastError());
     }

//2nd Thought
for(int k=0,j=0;j<count;j++)
     {
      if(ZigZag_Array[j]!=PLOT_EMPTY_VALUE && ZigZag_Array[j]!=0.0){
             ArrayInsert(ZigZag_Array2,ZigZag_Array,k,j,count);
             k++;
         }
         else
         Print("Error in copying Arrays" + GetLastError());
     }
 
Jan-HarmZ63: transfer elements from one array to another but only if the values meet specific criteria. (E.g. If the value is !=0).
Not compiled, not tested, just typed.
template<typename T>
int copyNonzero(T& to[], const T& from[], int iEnd=WHOLE_ARRAY, int iBeg=0){
   if(iEnd == WHOLE_ARRAY) iEnd = ArraySize(from);
   int nTo=0;
   for(; iBeg < iEnd; ++iBeg) if(from[iBeg] != 0){
      ArrayResize(to, nTo+1);
      to[nTo] = from[iBeg];
      ++nTo;
   }
   return nTo;
}
Not compiled, not tested, just typed.
When in doubt, THINK!
 

It's a lot easier to use dynamic collections for this kind of stuff. 

#include <arrays/arraydouble.mqh>
void OnStart() {
   int zz_handle = iCustom(NULL, PERIOD_H1, "Examples\\ZigZag", 20, 5, 3);
   CArrayDouble res_up, res_dn;
   double zz_up[], zz_dn[];
   ArraySetAsSeries(zz_up, true);
   ArraySetAsSeries(zz_dn, true);
   int upsize = CopyBuffer(zz_handle, 1, 0, 1000, zz_up);
   int dnsize = CopyBuffer(zz_handle, 2, 0, 1000, zz_dn);
   upsize = fmin(upsize, dnsize);
   for (int i = 0; i < upsize; i++) {
      double u = zz_up[i];
      double d = zz_dn[i];
      if (u != 0.0)
         res_up.Add(u);
      if (d != 0.0)
         res_dn.Add(d);
   }
   Print(res_up[0], " ", res_up[1]);
   Print(res_dn[0], " ", res_dn[1]);
}
 
nicholi shen:

It's a lot easier to use dynamic collections for this kind of stuff. 

Thank you for this. I will look into dynamic collections. This makes sense to me.


I appreciate your response.

 
William Roeder:
Not compiled, not tested, just typed. Not compiled, not tested, just typed.
When in doubt, THINK!

Thank you for your response William. I appreciate it.


Its easier than I thought :-)

 
nicholi shen:

It's a lot easier to use dynamic collections for this kind of stuff. 

Nice one. I'd recommend this approach.

Reason: