Arrayrelocate( not working as documented

It appears that I ought to be able to swap the last two items in a text array using

Array=arrayrelocate(Array,-1,-2,Separator)

i.e. move the last item (position -1) to be the penultimate item -2) — or vice-versa.

I can swap the penultimate and antepenultimate items, e.g.

arrayrelocate("A,B,C,D,E",-2,-3,",")  =>  A,B,D,C,E
arrayrelocate("A,B,C,D,E",-3,-2,",")  =>  A,B,D,C,E

but if either position parameter is -1, the function results in an error:

arrayrelocate("A,B,C,D,E",-2,-1,",")  =>  new item position is beyond end of array
arrayrelocate("A,B,C,D,E",-1,-2,",")  =>  original item position is beyond end of array

Adding the optional ‘count’ parameter before the separator doesn’t help.

There are other ways to achieve this which do work, e.g.

yoke(arrayrange(Array,1,-3,Separator),Separator,array(Array,-1,Separator)+Separator+array(Array,-2,Separator))

(the yoke function is needed in case the array has only two elements.) However, if arrayrelocate( worked as documented it would be an improvement on that, and, if used within a loop, hopefully quicker.

This is using version 10.2.0.b13 (3609).

Another alternative is to use the known extent of the array instead of -1 to indicate the last element. However, if that means using arraysize( to find that extent it’s still not ideal, although it does work:

arrayrelocate("A,B,C,D,E",-2,arraysize("A,B,C,D,E",","),",") => A,B,C,E,D
arrayrelocate("A,B,C,D,E",arraysize("A,B,C,D,E",","),-2,",") => A,B,C,E,D

but performing two different functions on the same array, one to count the elements and another to modify it, seems likely to be slower than relying on arrayrelocate( to do the whole operation.

So there’s nothing stopping arrayrelocate( accessing the last element of the array, it just wrongly objects to the use of -1 (but not -2, -3, etc.)

Thanks. On it.

That was quick! I look forward to that one then. Thank you.