Discussion:
[xquery-talk] How to Reverse an Array
Eliot Kimber
2015-07-17 13:26:50 UTC
Permalink
I'm still getting my head around dynamic functions and arrays in XQuery
3.1.

My question for today: easiest/best/most-efficient/cleverest way to
reverse an array.

You can't just use reverse() on an array, so how to produce a new array
that is the reverse of the input array?

Thanks,

Eliot

----
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com



_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Ghislain Fourny
2015-07-17 13:31:37 UTC
Permalink
Hi Eliot,

I'd just go to the sequence space and back:

array { reverse(array:flatten($array)) }

Kind regards,
Ghislain
Post by Eliot Kimber
I'm still getting my head around dynamic functions and arrays in XQuery
3.1.
My question for today: easiest/best/most-efficient/cleverest way to
reverse an array.
You can't just use reverse() on an array, so how to produce a new array
that is the reverse of the input array?
Thanks,
Eliot
----
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Ghislain Fourny
2015-07-17 13:37:43 UTC
Permalink
Hi Eliot,

Forget what I said: there is actually a function for this :-)

array:reverse($array).

http://www.w3.org/TR/xpath-functions-31/#func-array-reverse

But the approach in the other query works for any other function.

Kind regards,
Ghislain
Post by Ghislain Fourny
Hi Eliot,
array { reverse(array:flatten($array)) }
Kind regards,
Ghislain
Post by Eliot Kimber
I'm still getting my head around dynamic functions and arrays in XQuery
3.1.
My question for today: easiest/best/most-efficient/cleverest way to
reverse an array.
You can't just use reverse() on an array, so how to produce a new array
that is the reverse of the input array?
Thanks,
Eliot
----
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Michael Kay
2015-07-17 14:54:47 UTC
Permalink
Post by Ghislain Fourny
Hi Eliot,
array { reverse(array:flatten($array)) }
That doesn’t work if the array contains sequences (of length > 1) - flattening loses the sequence boundaries.

Michael Kay


_______________________________________________
***@x-query.com
http://x-query.com/mailman/list
Ghislain Fourny
2015-07-20 08:57:25 UTC
Permalink
Hi Mike,

Ah yes, it makes sense. Thanks for correcting me. This is likely to be
the reason why there is an array:reverse function in the first place.

I still had the JSONiq mindset where array members are single items. :-)

Kind regards,
Ghislain
Post by Michael Kay
Post by Ghislain Fourny
Hi Eliot,
array { reverse(array:flatten($array)) }
That doesn’t work if the array contains sequences (of length > 1) - flattening loses the sequence boundaries.
Michael Kay
_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/ta

Eliot Kimber
2015-07-17 13:39:38 UTC
Permalink
Ah--I missed that one--I'm bouncing between the 3.1 spec and a near-final
draft of Priscilla Walmsley's updated XQuery book.

Cheers,

E.
----
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com
Post by Ghislain Fourny
Hi Eliot,
http://www.w3.org/TR/xpath-functions-31/#func-array-reverse
Bye,
Benito
Post by Eliot Kimber
I'm still getting my head around dynamic functions and arrays in XQuery
3.1.
My question for today: easiest/best/most-efficient/cleverest way to
reverse an array.
You can't just use reverse() on an array, so how to produce a new array
that is the reverse of the input array?
Thanks,
Eliot
----
Eliot Kimber, Owner
Contrext, LLC
http://contrext.com
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Continue reading on narkive:
Loading...