# Compare one value to 100 element array?

397

How to do this without going one by one? Is there a way to compare for example

x to every value of array[] , when array has 100 elements?

or do we have to go one by one?

ArrayCompare() just compares a single element to a single element in the second array to be compared. I thought WHOLE_ARRAY was what I was looking for.

Any clues? Make a 100 element array with all values same as x sounds clunky

397

```Alert("comp1 ", ArrayCompare(tickarray0, tickarray, 0, 1, WHOLE_ARRAY));
Alert("comp2 ", ArrayCompare(tickarray0, tickarray, 0, 2, WHOLE_ARRAY));
Alert("comp3 ", ArrayCompare(tickarray0, tickarray, 0, 3, WHOLE_ARRAY));
Alert("comp4 ", ArrayCompare(tickarray0, tickarray, 0, 4, WHOLE_ARRAY));
Alert("comp5 ", ArrayCompare(tickarray0, tickarray, 0, 5, WHOLE_ARRAY));
Alert("comp6 ", ArrayCompare(tickarray0, tickarray, 0, 6, WHOLE_ARRAY));
Alert("comp7 ", ArrayCompare(tickarray0, tickarray, 0, 7, WHOLE_ARRAY));
Alert("comp8 ", ArrayCompare(tickarray0, tickarray, 0, 8, WHOLE_ARRAY));
Alert("comp9 ", ArrayCompare(tickarray0, tickarray, 0, 9, WHOLE_ARRAY));```
The only way?
7291

What about a binary search in a sorted array?
15917

nadiawicket:  ArrayCompare() just compares a single element to a single element in the second array to be compared. I thought WHOLE_ARRAY was what I was looking for. Any clues? Make a 100 element array with all values same as x sounds clunky.

Is there a way to compare for example x to every value of array[] , when array has 100 elements?
Alert("comp8 ", ArrayCompare(tickarray0, tickarray, 0, 8, WHOLE_ARRAY));
How to do this without going one by one? or do we have to go one by one?
1. That function compares two arrays. Makes no sense when you have one value. You want to search the array.
2. If the array isn't ordered, yes you have to. It's not hard!
```#define INDEX uint   // Zero based
/** Linear search an _array_ for a specific _value_. @returns INDEX
* of the first element found with _value_ or _end_ if not found.          */
template<typename Ti1, typename Ti2>
static INDEX      find(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
const Ti2& value){
while(iBeg != iEnd && !(inp[iBeg] == value) )   ++iBeg;
return iBeg;
}
/** Linear search an _array_ for a specific _value_, i.e.\ where the supplied
* _pred_ `class` or `struct` method `is_member(T)` returns true.
* @returns INDEX of the first element found or _end_ if not found.        */
template<typename Ti, typename UnaryPredicate>
static INDEX      find_if(const Ti& inp[], INDEX iBeg, INDEX iEnd,
UnaryPredicate& pred){
while(iBeg != iEnd && !pred.is_member(inp[iBeg] )) ++iBeg;
return iBeg;
}```
3. If the array is sorted, you can use a binary search. See Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
```/** Returns the index in a __sorted__ _array_, the first element greater
* than or equal to _value_. If all values are less than _value_, returns
* _end_.                                                                  */
template<typename Ti1, typename Ti2>
INDEX      lower_bound(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
const Ti2& value){
COUNT    len   = iEnd - iBeg;
while(len > 0){
COUNT    half  = len >> 1;
INDEX    middle = iBeg + half;
if(inp[middle] < value){
iBeg  = middle + 1;
len   = len - half - 1;
}
else  len = half;
}
return iBeg;
}
/** Returns the index in a __sorted__ _array_, the first element which does
* not compare smaller than _value_ using the supplied _pred_ `class` or
* `struct` method `is_before(T, T)` which creates a [strict weak
* ordering](http://simple.wikipedia.org/wiki/Strict_weak_ordering "Strict
* weak ordering - Simple English Wikipedia, the free encyclopedia"). If no
* value is larger, returns _end_.                                         */
template<typename Ti1, typename Ti2, typename BinaryPredicate>
INDEX      lower_bound(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
const Ti2& value, BinaryPredicate& pred){
COUNT    len   = iEnd - iBeg;
while(len > 0){
COUNT    half  = len >> 1;
INDEX    middle = iBeg + half;
if(pred.is_before(inp[middle], value) ){
iBeg  = middle + 1;
len   = len - half - 1;
}
else  len = half;
}
return iBeg;
}
```

4. or use a tree, not an array
397

Carl Schreiber:
What about a binary search in a sorted array?

Problem is the

### ArrayBsearch() only returns closest value to what you are searching. I need it to return a plain yes or no. Looks like I'll have to use the manual one by one method for a 500 part array. The other suggestions are a little bit out of my league at the moment lets see how it goes.

```#define INDEX uint   // Zero based
/** Linear search an _array_ for a specific _value_. @returns INDEX
* of the first element found with _value_ or _end_ if not found.          */
template<typename Ti1, typename Ti2>
static INDEX      find(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
const Ti2& value){
while(iBeg != iEnd && !(inp[iBeg] == value) )   ++iBeg;
return iBeg;
}
/** Linear search an _array_ for a specific _value_, i.e.\ where the supplied
* _pred_ `class` or `struct` method `is_member(T)` returns true.
* @returns INDEX of the first element found or _end_ if not found.        */
template<typename Ti, typename UnaryPredicate>
static INDEX      find_if(const Ti& inp[], INDEX iBeg, INDEX iEnd,
UnaryPredicate& pred){
while(iBeg != iEnd && !pred.is_member(inp[iBeg] )) ++iBeg;
return iBeg;
} ```
looks like it lemme see how i figure all of this out
397

whroeder1:
1. That function compares two arrays. Makes no sense when you have one value. You want to search the array.
2. If the array isn't ordered, yes you have to. It's not hard!
3. If the array is sorted, you can use a binary search. See Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
4. or use a tree, not an array
Thanks
397

whroeder1:
1. That function compares two arrays. Makes no sense when you have one value. You want to search the array.
2. If the array isn't ordered, yes you have to. It's not hard!
3. If the array is sorted, you can use a binary search. See Sort multiple arrays - MQL4 and MetaTrader 4 - MQL4 programming forum
4. or use a tree, not an array

If you would explain a little, it would help a lot because the official documentation I didn't understand and its been like 10 hours. I'm stuck at how to actually enable/use templates, the official examples I just cant figure out.

```   #define INDEX uint
// Zero based
/** Linear search an _array_ for a specific _value_. @returns INDEX
* of the first element found with _value_ or _end_ if not found.          */
template<typename Ti1, typename Ti2>
static INDEX
find(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
const Ti2& value){
while(iBeg != iEnd && !(inp[iBeg] == value) )   ++iBeg;
return iBeg;```
`#define INDEX uint   // Zero based`

Previous item; I just leave that as is and placed it on the top of file.

`template<typename Ti1, typename Ti2>`

Previous item; I substitutue ti1 and ti2 for the actual values?

`static INDEX     `

You are pulling this one here instead of at the top of the file again like #Define. I just put the array[] index here?

5077

Problem is the

### ArrayBsearch() only returns closest value to what you are searching. I need it to return a plain yes or no. Looks like I'll have to use the manual one by one method for a 500 part array. The other suggestions are a little bit out of my league at the moment lets see how it goes.

looks like it lemme see how i figure all of this out

```template<typename T1,typename T2>
bool ArrayFind(const T1 &array[],const T2 value,int begin=0,int count=WHOLE_ARRAY)
{
int size=ArraySize(array);
if(count==WHOLE_ARRAY)
count=size-begin;
if(begin+count>size)
return(false);
for(int i=begin;i<begin+count;i++)
if(array[i]==value)
return(true);
return(false);
}```
397

Ernst Van Der Merwe:

If this works, you saved me like a week.
```template<typename T1,typename T2>
bool ArrayFind(const T1 &array[],const T2 value,uint begin=0,uint count=WHOLE_ARRAY)
{
int size=ArraySize(array);
if(count==WHOLE_ARRAY)
count=size-begin;
if(begin+count>size)
return(false);
for(int i=begin;i<count;i++)
if(array[i]==value)
return(true);
return(false);
}```
so
`template<typename T1,typename T2>`

Previous item is where I put the Ti for the value of the first thing I want to compare? And t2 for the second parameter?

5077

If this works, you saved me like a week.so

Previous item is where I put the Ti for the value of the first thing I want to compare? And t2 for the second parameter?

T1 is where the array is passed to and T2 is the value to be found.

```   double array1[]={1.2341,1.4789,1.6434,1.9487};
double value1=1.6434;

if(ArrayFind(array1,value1))
Print("Value found!");

int array2[]={31,76,94,27,438,5};
double value2=5;

if(ArrayFind(array2,value2,2,WHOLE_ARRAY))
Print("Value found!");
```
397

Ernst Van Der Merwe:

Only the function is used.

Got some progress, I can't believe