Is there a "in" function in MQL4?

 

Is there a "in" function in MQL4? Something like

 if( Var in 1,2,3 ) ...

 It is pretty common in other languages but I haven't been able to find it in MQL4 yet.

 
Not common at all (I can only think of Lisp, maybe APL.) All others use a function. Make one.
int ValueFindArray(double ar[], double v, int nAr=WHOLE_ARRAY, int iBeg=0){
   if(nAr == WHOLE_ARRAY)  nAr = ArraySize(ar) - iBeg;
   for(; iBeg < nAr; iBeg++) if(ar[iBeg] == v) return(iBeg);
   return(EMPTY);
}
/////////////////
double values[] = {1, 2, 3};
if(ValueFindArray(values, Var) != EMPTY){ // Var in 1,2,3 ) ...
 

Well, that's a big surprise. I know both SQL and SAS support it, and since it is relatively simple and much used I would expect it to be very common.

But a function it will have to be. Thanks for the in info WHRoeder. 

 

Although an array is an option, I find it not so userfriendly in this case. So made a function for it:

 

// Find string in list of strings
bool ListFind( string SearchFor , string SearchIn , string SearchSeparator = "," )
{

  bool Answer      = false;
  bool Done        = false;
  int  SearchStart = 0;

  while( Done == false )
  {

    int SeparatorPosition = StringFind( SearchIn , SearchSeparator , SearchStart );
    if ( SeparatorPosition == -1 ) string StringPart = StringSubstr( SearchIn , SearchStart );
    else                                  StringPart = StringSubstr( SearchIn , SearchStart , ( SeparatorPosition - SearchStart ) );

    if ( StringStrip( SearchFor ) == StringStrip( StringPart ) ) Answer = true;

    if ( SeparatorPosition == -1  || Answer == true ) Done = true;
    else SearchStart = SeparatorPosition + 1;
   
  }

 return( Answer );
 
}

 
// Remove leading and trailing blanks
string StringStrip( string StringToStrip )
{
 return( StringTrimLeft( StringTrimRight( StringToStrip ) ) );

 

The function ListFind returns true or false, depending on whether the string could be found or not.

Examples:

bool B = ListFind( "dog" , "cat,dog,mouse" ) 

bool B = ListFind( "dog" , "cat , dog , mouse" ) 

bool B = ListFind( "dog" , "cat / dog / mouse" , "/" ) 

 

The above examples could have been done by using StringFind,  but you would run into trouble with the following example: StringFind( "100 200 300" , "20" )

It would tell you that your string starts at position 4. 

ListFind( "20" , "100 200 300" , " " )  would give you the correct anwser. 

*edit* Oops... used "string B" instead of "bool B". Corrected that error. *edit*

Reason: