ArrayBSearch help...

 

Ok, what the heck does ArrayBSearch return if it doesn't find the value??

The doc says:

"Returns the index of the first occurrence of a value in the first dimension of array if possible, or the nearest one, if the occurrence is not found."

What the heck does that mean - "or the nearest one, if the occurrence is not found."??????? Shouldn't it just return a NULL (or at least a -1 allowing for mt4 funkiness) like a normal c bsearch?

Hoping someone can tell me so I don't have to write a test EA to find out manually.

Thanks mucho,

CA

 

Hmm...

I did the following:

double foo[10] = {1,2,3,4,5,6,7,8,9,10};

ArraySort(foo);

int index = ArrayBsearch(foo,4);

Print ("found: "+index);

index = ArrayBsearch(foo,11);

Print ("not found: "+index);[/CODE]

And got:

found: 3

not found: 9

And then did this:

[CODE] double foo[10] = {1,2,3,4,5,6,7,8,9,10};

ArraySort(foo);

int index = ArrayBsearch(foo,4);

Print ("found: "+index);

index = ArrayBsearch(foo,-1);

Print ("not found: "+index);

And got:

found: 3

not found: 0

So, it seems that ArrayBsearch returns either the first or the last index if your number isn't found. Thats kind of dumb - don't you now have to make sure that the first or last index doesn't actually contain the item you are searching for?

I must be missing something here...

Any help would be greatly appreciated!

Thanks,

CS

 

Talking to myself.

Ok, so it seems that I'm the only one who has a challenge with this implementation.

I've gone ahead and written myself this little function. Maybe someone else will find it useful. It behaves more like I would expect a search function to behave. If the an item is found, it returns the first position it is found in. If it isn't found, it returns -1, so that you know it isn't there.

//////////////////////////////////////////////////////////////

// Function: SearchArray(ArrayToSearch, ValueToSearch,NumToCount,StartFrom,Dir)

// Return Values:

// Positive Number = position of value

// -1 = not found

//////////////////////////////////////////////////////////////

int SearchArray(double ArrayToSearch[],double ValueToSearch, int NumToCount=WHOLE_ARRAY, int StartFrom=0,int Dir=MODE_ASCEND)

{

Print ("searching for "+ValueToSearch+" counting "+NumToCount+" starting from "+StartFrom+" in Dir "+Dir);

int LastIndexNo = ArraySize(ArrayToSearch)-1;

ArraySort(ArrayToSearch,NumToCount,StartFrom,Dir);

int index = ArrayBsearch(ArrayToSearch,ValueToSearch,NumToCount,StartFrom,Dir); // get the position

Print ("This is the Index returned by bsearch: "+index);

if (ArrayToSearch == ValueToSearch) return (index); //checking for actual match and not "closest index"

else return (-1);

}

Is this a good thing, or a bad thing, when I'm retraining the tools, rather than retraining myself? haha...

Anyway, hopefully someone will think this is useful. I sure do.

 

Changing threads!

See:

https://www.mql5.com/en/forum/174920

for more array functions.

 
lol. you weren't talking to yourself. you were tallking to some guy in future. 14 years later and this is still valuable. Thanks!
 
cubesteak: I did the following:
[CODE] double foo[10] = {1,2,3,4,5,6,7,8,9,10};
index = ArrayBsearch(foo,-1);
not found: 0

So, it seems that ArrayBsearch returns either the first or the last index if your number isn't found. Thats kind of dumb

That is not what it did. It wasn't dumb. The nearest value to negative number is the lowest positive one. So It returned zero (index to 1.) Try it again on n.4 and n.6


Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
          Messages Editor