is there any easier way to write this: - page 2

 

dr.Rez:

and I did not understand this : 

  1. There are no mind readers here.
That's probably because you are no mind reader either.
 

That code just count the number of times a value is present in the given array, which is basically the same "functionality" of the logical OR that you used. However, in your case, you don't need to count the value occurrences, but you just need to know if the value is present or not in the array. So, you can just stop as soon as the value is found, like this:

template<typename T>
bool IsValueInArray(const T&   rcArray[],
                    const uint cIndexBegin,
                    const uint cIndexEnd,
                    const T&   rcValue)
{
    bool found = false;
    
    for (uint i = cIndexBegin; (i < cIndexEnd) && (!found); i++)
    {
        if (rcArray[i] == rcValue)
        {
            found = true;
        }
    }

    return (found);
}

void OnStart()
{
    const int array[] = {12, 83, 65, 15};
    int value_1 = 65;
    int value_2 = 45;
    
    /* Value found, DoSomething is executed */
    if (IsValueInArray(array, 0, ArraySize(array), value_1))
    {
        DoSomething();
    }
    
    /* Value not found, DoSomething is not executed */
    if (IsValueInArray(array, 0, ArraySize(array), value_2))
    {
        DoSomething();
    }
}

This is equivalent to do the logical OR. However, this requires to define a new array each time for every condition to be checked, which is not convenient. It may have sense to use it if you have to check that condition for many many times and the condition is very long (for example, you have to compare your value to 10 numbers), so you can create a function like this to easily perform the operation:

bool CheckCondition(const int cValue)
{
    static const int array[] = {12, 83, 65, 15};
    
    return IsValueInArray(array, 0, ArraySize(array), cValue);
}

So the previous code will become:

void OnStart()
{
    int value_1 = 65;
    int value_2 = 45;
    
    /* Value found, DoSomething is executed */
    if (CheckCondition(value_1))
    {
        DoSomething();
    }
    
    /* Value not found, DoSomething is not executed */
    if (CheckCondition(value_2))
    {
        DoSomething();
    }
}

Of course you can also just create a "stupid" function like:

bool CheckCondition(const int cValue)
{
    return ((cValue == 12) ||
            (cValue == 83) ||
            (cValue == 65) ||
            (cValue == 15));
}

Which does the same thing, but using the previous approach you can write a shorter and less error-prone function in case there are lots of values to be compared (imagine if you have to compare the value to 10 different numbers).


I hope it'll be useful for you.

 
dr.Rez: d I did not understand this :
  1. There are no mind readers here.
You are the one that posted the misleading example initially
dr.Rez: I didnt mean it . let change my numbers: for axample 12,83,65 , 15. its not about Continuous numbers.


Emanuele Bellocchia: you don't need to count the value occurrences, but you just need to know if the value is present or not in the array. So, you can just stop as soon as the value is found
True, but present is just count non-zero. Counting is more general.
There is count in the C++ template library but no equivalent of present.
 
whroeder1:



True, but present is just count non-zero. Counting is more general.
There is count in the C++ template library but no equivalent of present.

Yes of course but, by stopping immediately, you will save some useless loop cycles since there is no need to continue if the value has been found. However, I agree that, with small arrays like those ones, this is completely negligible.

The standard C++ libray does check for presence. It has the std::find function that checks for presence in a generic container and stops immediately if it is found, returning its position (iterator). It's similar to what I did, with the difference that I've just returned true or false instead of the position, basically in C++ it'll be like this:

return (std::find(vector.begin(), vector.end(), value) != vector.end());

But for the usage described by the topic author the position was not needed, so I didn't return it.

Anyway, it's just a little different approach for doing the same thing :-)

 
Marco vd Heijden:
That's probably because you are no mind reader either.

Thank you Marco 
you have  always helped me .
whroeder1:
You are the one that posted the misleading example initially






Thank you whroeder1
My English is not good and I read,write and understand it  with a little difficulty. 
and also I am beginner in mql.
when you told me  "There is no mind readers here", I didn't understand you were talking about the way I asked my question .


by the way ,  thank you for your help.
 

You can also use a string. It's much more easy, but you'll get to worry about var conversion.

void FindNumbers(int A)
{
string numbers = "1;2;3;4;5;6;4;5;98;514515145;9884220";
string number[];
////////
StringSplit(numbers,StringGetCharacter(";",0),number); 
////////
for(int i=0;i<ArraySize(number);i++) { if ((string)A == number[i]) { printf("Value %g found ! @ plot %g",(int)number[i],i); break; } } 
}
/////////// Function call :
FindNumbers(98);
Reason: