This is a question about the behavior of fn!index() function. The syntax is fn!i

This is a question about the behavior of fn!index() function. The syntax is fn!index(array, indices, defaultValToReturn).
The defaultValToReturn is of AnyType. Documentation mentions that default value should be of the same type as array elements. I find the behavior of index function strange in the below case:
fn!index( {4,5,6}, 1000, {}) -- As expected it returns a empty list i.e List of Variant since array size is less than 1000.
Now try, fn!index( {4,5,6}, {1000}, {} ) - This returns 4 - the first element in the array. I did not expect this result.
Now try, fn!index( {4,5,6}, {1000}, null ) - This returns nothing as expected.

Why does it behave in such manner for case 2? It always returns the first element whenever a multiple indices are provided and the default value is also an empty list.
Also try: fn!index({4,5,6}, {2,1000}, {}) - this returns 5,4. The 4 is the first element of the array.
I...

OriginalPostID-184786

OriginalPostID-184786

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer
    To add to the above, it looks like when it's given an array of indices and an array of default values, for each element in the index array that is not found it will return the equivalent index in the default values array. If it runs out of elements in the default values array, it starts to return the elements of the first array. if you pass in an array of indexes and an empty array for defaults, it immediately runs out of elements in the default array and so returns an element of the first array for each index that is not found.

    Have a look at the following example, and add or remove elements to the defaults array to see the behaviour described above.

    index(
    {4,5,6},
    {7,1,34},
    {0,1}
    )
Reply
  • 0
    Certified Lead Developer
    To add to the above, it looks like when it's given an array of indices and an array of default values, for each element in the index array that is not found it will return the equivalent index in the default values array. If it runs out of elements in the default values array, it starts to return the elements of the first array. if you pass in an array of indexes and an empty array for defaults, it immediately runs out of elements in the default array and so returns an element of the first array for each index that is not found.

    Have a look at the following example, and add or remove elements to the defaults array to see the behaviour described above.

    index(
    {4,5,6},
    {7,1,34},
    {0,1}
    )
Children
No Data