arraylast( in theory
David Thompson
dthmpsn1 at uiuc.edu
Sat Feb 23 09:28:02 PST 2008
>I don't know if it's a bug or not, but I would expect that arrylast(
>would respond much as a regular array( if there is no more than a
>single element in an array.
>
>Take the string "abcd"
>
>array( "abcd",1,".") returns "abcd"
>array( "abcd",2,".") returns ""
>arrayfirst( "abcd",".") returns "abcd"
>arraylast( "abcd",".") returns "abcd"
>
>I would expect arraylast( to return "" although I can see all the
>arguments for otherwise.
>
>But consider:
>extract( "abcd",".",1) returns "abcd"
>extract( "abcd",".",2) returns ""
>"abcd" [1,"."] returns "abcd"
>"abcd" [".",-1] returns ""
>
>So the theoretically based question is, if there is one or no element
>in an array, does the array actually have a last element, or is the
>last element empty?
>
>Array sizes can vary a lot from nothing to many elements. Finding the
>first is easy, but grabbing the last only if there are multiple
>elements seems to require something like:
>?(arraysize("abcd",".") > 1,array("abcd",arraysize("abcd","."),"."),"")
>
>The appeal of arraylast( is being to reliably get the last element
>without having to calculate where it is or if it even exists.
I don't think "last" implies more than one any more than "first"
implies more than one. As I would interpret that, when there is only
one element, it is both first and last. If you want arraylast( to
return a blank when there is only one element, then to be consistent
you should also want arrayfirst( to return a blank in that instance.
To grab the last, only if there are multiple elements, you could use
"abcd"["-.",-1][".",-1][2,-1]
Dave
More information about the Qna
mailing list