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 ) ...
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*
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.