# IndexLength

new to Analytica 5.0

## IndexLength(I)

Returns the number of elements in index «I».

IndexLength is closely related to the Size function, but differs in that `IndexLength(I)` gives the size of the index `I`, whereas `Size(a)` gives the size of the value `a`. For a simple, non-dynamic index, these are the same, but they are usually different in the case where `a` is a multi-dimensional self-indexed array, or where the size of a dynamic index is requested from within a dynamic loop.

`IndexLength(I)` is equivalent to `Size(IndexValue(I))`, and to `Sum(1, I)`.

## Explanatory Video

Video: Why the IndexLength function is probably better than alternatives (4 minutes)

## Examples

`Index The_Date := Sequence(20-Jan-2009, 19-Jan-2017) Do IndexLength(The_Date) → 2922`

Consider the case where variable `A` is a self-indexed array:

```Index Digit := 0..9
Variable A := [0, 10, 20, 30] + Digit
Size(A) → 40
IndexLength(A) → 4
```

Suppose you need the length of the Time index from within a dynamic loop:

```Index Time := 2017..2025
Dynamic(Size(Time)) → Array(Time, [1, 1, 1, 1, 1, 1, 1, 1, 1])
Dynamic(IndexLength(Time)) → Array(Time, [9, 9, 9, 9, 9, 9, 9, 9, 9])
```

Inside a dynamic loop, the value of Time is the single time point, hence its Size is always 1. Hence, you should use IndexLength when you really want the length of the Time index.

## History

IndexLength was introduced as a built-in function in Analytica 5.0. Prior to Analytica 5.0, you can define your own User-Defined Function by:

`Function IndexLength(I : Index) := Size(I)`

or use the more verbose expression `Size(IndexValue(I))` when you need the size of the index.