First non-null value?

Friday, 14 September 2018 · 4 messages
GS
Graydon Saunders14 Sep 2018 00:18 #
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
LQ
Liam R. E. Quin14 Sep 2018 10:38 #
Quoted text · Graydon Saunders · 17 lines
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]
Quoted text · Graydon Saunders · 1 line
>
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.
AB
Andy Bunce14 Sep 2018 10:51 #
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)
Quoted text · Liam R. E. Quin · 39 lines
On 14 September 2018 at 07:38, Liam R. E. Quin <liam at 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. > >
GS
Graydon Saunders14 Sep 2018 16:30 #
Quoted text · Andy Bunce · 5 lines
On Fri, Sep 14, 2018 at 4:51 AM Andy Bunce <bunce.andy at 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

⚡Generated with XQuery