How to get index of last element in array / index of the first empty value?

 

Hello!

is there a quick and nice way to get the index of the last element in an array or the index of the first empty value?

One way of course would be the use of an index variable but maybe there is a better way.

I also tried ArraySize but I initialize my Arrays at the beginning with a size of i.e. 10 and even though the array still does not contain any values, ArraySize returns 10 and not 0 or NULL.

Any ideas?

 
syrvn 0:

Hello!

is there a quick and nice way to get the index of the last element in an array or the index of the first empty value?

One way of course would be the use of an index variable but maybe there is a better way.

I also tried ArraySize but I initialize my Arrays at the beginning with a size of i.e. 10 and even though the array still does not contain any values, ArraySize returns 10 and not 0 or NULL.

Any ideas?

A dynamic array will give you the total count of elements through a single method call.

https://www.mql5.com/en/docs/standardlibrary/datastructures/carray/carraytotal

For fixed arrays, I know of no "nice easy" way. Maybe loop backwards from the end or remember the value as you suggested.

Documentation on MQL5: Standard Library / Data Collections / CArray / Total
Documentation on MQL5: Standard Library / Data Collections / CArray / Total
  • www.mql5.com
Standard Library / Data Collections / CArray / Total - Reference on algorithmic/automated trading language for MetaTrader 5
 
Cool, did not know of CArray. thanks for pointing that out. I will give it a try!
Reason: