let $possible1 as xs:string* := (: go looking for a value via one route :) let $possible2 .... (: all the other routes in preference order :) .... let $foundIt as xs:string := ($possible1,$possible2,$possible3,$possible4,$possible5,'FAILED')[1]
This works nicely in terms of "I got the value by the least-bad route". What I'm blanking on is "how do I tell which was the first possibility to match?" without resorting to sprawl of if-then-else statements. I have the idea that there must be a compact way but I have no idea what it is if there is.
Any suggestions?
Thanks! Graydon
On Thu, 2018-09-13 at 16:18 -0400, Graydon Saunders wrote:
let $possible1 as xs:string* := (: go looking for a value via one route :) let $possible2 .... (: all the other routes in preference order :) .... let $foundIt as xs:string := ($possible1,$possible2,$possible3,$possible4,$possible5,'FAILED')[1]
This works nicely in terms of "I got the value by the least-bad route". What I'm blanking on is "how do I tell which was the first possibility to match?" without resorting to sprawl of if-then-else statements. I have the idea that there must be a compact way but I have no idea what it is if there is.
Don't use variables - just construct a sequence, let $possibles as xs:string := ( stuff to make possible1, stuff to make possible 2, . . . 'fallback value' )[1]
Liam
If you put your "possibles" in an array rather than a sequence then the index of the first non-empty item identifies the match.
let $results := [$possible1,$possible2,$possible3,$possible4,$ possible5,'FAILED']
let $index:= array:fold-left($results, -1, function($acc,$this){ if($acc gt 0)then $acc else if (exists(array:get($results,-$acc))) then -$acc else $acc -1 })
let $foundIt:= array:get($results,$index)
This seems a bit tricksy, using the BaseX specific higher order function hof:until [1] is cleaner
let $index:= hof:until( function($index){ exists(array:get($results,$index)) }, function($index){ $index+1 }, 1)
/Andy
[1] http://docs.basex.org/wiki/Higher-Order_Functions_Module#hof:until
On 14 September 2018 at 07:38, Liam R. E. Quin liam@fromoldbooks.org wrote:
On Thu, 2018-09-13 at 16:18 -0400, Graydon Saunders wrote:
let $possible1 as xs:string* := (: go looking for a value via one route :) let $possible2 .... (: all the other routes in preference order :) .... let $foundIt as xs:string := ($possible1,$possible2,$possible3,$possible4,$possible5,'FAILED')[1]
This works nicely in terms of "I got the value by the least-bad route". What I'm blanking on is "how do I tell which was the first possibility to match?" without resorting to sprawl of if-then-else statements. I have the idea that there must be a compact way but I have no idea what it is if there is.
Don't use variables - just construct a sequence, let $possibles as xs:string := ( stuff to make possible1, stuff to make possible 2, . . . 'fallback value' )[1]
Liam
-- Liam Quin, https://www.holoweb.net/liam/cv/ Web slave for vintage clipart http://www.fromoldbooks.org/ Available for XML/Document/Information Architecture/ XSL/XQuery/Web/Text Processing/A11Y work/training/consulting.
On Fri, Sep 14, 2018 at 4:51 AM Andy Bunce bunce.andy@gmail.com wrote:
If you put your "possibles" in an array rather than a sequence then the index of the first non-empty item identifies the match.
Thank you! That does it nicely.
It's going to be a little while before I feel like I've comprehended the hof:until() example, but "use an array" is clearly the way to go, here, and I had flat forgotten there _were_ arrays.
-- Graydon
basex-talk@mailman.uni-konstanz.de