Re: How to convert sequence of characters to numbers

new topic     » goto parent     » topic index » view thread      » older message » newer message
jaygade said...

Right, but is it fast. That was one of the OP's original criteria.

Dang it, you're right. I just did some tests on over 500,000 iterations; the method proposed by you, Spock, and Kat is over 25 times faster than using my regular expression. sad

However, using a regular expression makes it easier to:

  • quickly parse the 'number' out of a string or text file
  • validate the input on one pass: it either matches or it doesn't
  • allow for any number of variations and/or strictness in the input, e.g.
    • 'year' can only be 1970-2012
    • 'month' can only be 01-12
    • 'day' can only be 01-31
    • 'hour' can only be 00-23
    • 'minute' and 'second' can only be 00-59

Here is the function I used against mine for testing...

function get_parts( sequence string ) 
     
    string -= '0' 
     
    sequence parts = repeat( 0, 6 ) 
    parts[1] = (string[1] * 1000) + (string[2] * 100) + (string[3] * 10) + string[4] 
    parts[2] = (string[5] * 10) + string[6] 
    parts[3] = (string[7] * 10) + string[8] 
    parts[4] = (string[9] * 10) + string[10] 
    parts[5] = (string[11] * 10) + string[12] 
    parts[6] = (string[13] * 10) + string[14] 
     
    return parts 
end function 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu