Arrayrange( extracting elements from the end of an array

arrayrange(Array,FirstItem,LastItem,Separator) works as expected in most situations, but when using negative indices to count from the end, if the number of elements requested is greater than the total number in the array, it returns the null string instead of the whole array as I would expect:

arrayrange("A",-2,-1,",")       ==>
arrayrange("A,B,C",-4,-1,",")   ==>

If there are at least as many elements as requested it works as expected:

arrayrange("A,B",-2,-1,",")     ==>  A,B
arrayrange("A,B,C",-2,-1,",")   ==>  B,C
arrayrange("A,B,C,D",-4,-1,",") ==>  A,B,C,D

Starting from the beginning of the array instead of the end, it works as expected in all cases, as far as I can tell. If more elements are requested than supplied, the result is the whole array:

arrayrange("A",1,2,",")         ==>  A
arrayrange("A,B",1,2,",")       ==>  A,B
arrayrange("A,B,C",1,2,",")     ==>  A,B
arrayrange("A,B,C",1,4,",")     ==>  A,B,C
arrayrange("A,B,C,D",1,4,",")   ==>  A,B,C,D

I would have expected that asking for either the first x or last x elements should give the same result when the array has between 1 and x elements.

This is what is called a “can of worms”. In addition to the problem you found with negative indexes used with text arrays, using this function with data arrays also fails if the array indexes are outside of bounds in either the positive or negative direction.

These problems actually required this function to be almost completely rewritten! I believe it will now handle any combination of indexes you throw at it, positive or negative.

After finding these problems I was worried that maybe text funnels might have similar problems - but my tests show that they don’t. Phew.

Thanks for looking at it so quickly. It is always a relief to establish, however rarely, that unexpected behaviour is Panorama’s doing and not mine.