Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Fast way to remove all non-numerical chars from a string

From: pwawood::gmail::com at: 22-Sep-2007 11:32

In addition to Carl's solution, you could try parse, based on Romano's advice that "if you want speed, parse is your friend".
>> input-string: "(250) 764-0929"
== "(250) 764-0929" ;; Create a bitset! of numeric digits to use in parse
>> digit: charset [#"0" - #"9"]
== make bitset! #{ 000000000000FF03000000000000000000000000000000000000000000000000 } ;; create a string! the length of the input-string in which to collect the output
>> output-string: make string! length? input-string
== "" ;; parse the input-string to collect only numeric digits
>> parse input-string [ any [ copy next-digit digit (insert tail
output-string next-digit) | skip]] == true ;;check the result
>> probe output-string
2507640929 == "2507640929" If all your strings are very long, it might be worth using a temporary hash! in which to collect the digits within the parse and then convert it to a string afterwards. (The only problem with using hash! is that it has been removed from Rebol 3 and I don't know enough to tell if it successor can be used in the same way).
>> temp: make hash! length? input-string
== make hash! []
>> parse input-string [ any [ copy next-digit digit (insert tail temp
next-digit) | skip]] == true
>> output-string: to string! temp
== "2507640929" I haven't compared the speed of the two approaches. Regards Peter On Saturday, September 22, 2007, at 06:45 am, Kai Peters wrote: