[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: [XSLT 1.0] How to get the last number in a string

Subject: Re: [XSLT 1.0] How to get the last number in a string of numbers?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 23 Dec 2009 15:15:40 GMT
Re:  [XSLT 1.0] How to get the last number in a string
> I have a variable that contains a bunch of numbers:

or rather you have variable containg a result tree fragment
corresponding to a root node with child a text node
with string value consisting of digits and spaces.

You've given no context to your real problem but the solution may well
be not to start from here but to generate a different initial structure.


> I want the last number fast, i.e., I want it in constant time, not
> O(n), where n is the length of the list of numbers. 

even getting the length of a literal string might (depending on
processor optimisations) take time proportional to the length of the
string.  A processor may use an optimsed string representation that lets
it know there are no surrogate pairs (if it it using utf16 internally) or
it may not, but if it doesn't, then doing more or less any operation has
complexity at least O(n).

Hwever assuming you can get the length of the string in negligable time
then you can get the last number in time proportional to the number of
digits in that number by starting with
substring($numbers,string-length($numbers))
to get the last character, and then backing up until you get the first
non digit and re-assembling the final number.

David

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.