Author Topic: Convert String to Number  (Read 622 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1361
Convert String to Number
« on: March 25, 2022, 02:26:39 PM »
Here is the challenger, convert any string into a number, this could be simple strings like:
"100"
"100.45"

or more complex strings like:
"THIS IS A TEST 100 TEST"

For the last example, the value should return 100.

Additionally, all MText and other formatting should be ignored.

Here is my entry:
Code: [Select]
  (defun Str2Num (Str / List_Check List_Numbers List_Characters Results Character)
    ;Supporting Functions
    (defun LM:UnFormat ( str mtx / _replace rx )
      ;;-------------------=={ UnFormat String }==------------------;;
      ;;                                                            ;;
      ;;  Returns a string with all MText formatting codes removed. ;;
      ;;------------------------------------------------------------;;
      ;;  Author: Lee Mac, Copyright 2011 - www.lee-mac.com       ;;
      ;;------------------------------------------------------------;;
      ;;  Arguments:                                                ;;
      ;;  str - String to Process                                   ;;
      ;;  mtx - MText Flag (T if string is for use in MText)        ;;
      ;;------------------------------------------------------------;;
      ;;  Returns:  String with formatting codes removed            ;;
      ;;------------------------------------------------------------;;
 
      (defun _replace ( new old str )
        (vlax-put-property rx 'pattern old)
        (vlax-invoke rx 'replace str new)
      )
      (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
        (progn
          (setq str
            (vl-catch-all-apply
              (function
                (lambda ( )
                  (vlax-put-property rx 'global     actrue)
                  (vlax-put-property rx 'multiline  actrue)
                  (vlax-put-property rx 'ignorecase acfalse)
                  (foreach pair
                    '(
                      ("\032"    . "\\\\\\\\")
                      (" "       . "\\\\P|\\n|\\t")
                      ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
                      ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
                      ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
                      ("$1"      . "[\\\\]({)|{")
                    )
                    (setq str (_replace (car pair) (cdr pair) str))
                  )
                  (if mtx
                    (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
                    (_replace "\\"   "\032" str)
                  )
                )
              )
            )
          )
          (vlax-release-object rx)
          (if (null (vl-catch-all-error-p str))
            str
          )
        )
      )
    )
    ;End Supporting Functions
   
    (setq Str (LM:Unformat Str T))
    (setq List_Check (list 45 46 48 49 50 51 52 53 54 55 56 57)
          List_Characters (vl-string->list Str)
    )
    (foreach Character List_Characters
      (if (member Character List_Check)
        (setq List_Numbers (cons Character List_Numbers))
        (setq List_Numbers (cons 32 List_Numbers))
      )
    )
    (setq Results (vl-string-trim " " (vl-list->string (reverse List_Numbers))))
    (if (= (substr Results 1 1) ".")
      (setq Results (strcat "0" Results))
    )
    (read Results)
  )

JohnK

  • Administrator
  • Seagull
  • Posts: 10158
Re: Convert String to Number
« Reply #1 on: March 25, 2022, 03:50:03 PM »
This is very similar to one of our challenges already (String index).
https://www.theswamp.org/index.php?topic=57413.0

TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

cmwade77

  • Swamp Rat
  • Posts: 1361
Re: Convert String to Number
« Reply #2 on: March 25, 2022, 05:50:27 PM »
This is very similar to one of our challenges already (String index).
https://www.theswamp.org/index.php?topic=57413.0
Similar, but I would say a little different, as the indexer in the challenge doesn't necessarily account for mtext formatting and it doesn't actually return the value of the number as near as I can tell.

This one should output just the number from the text.

JohnK

  • Administrator
  • Seagull
  • Posts: 10158
Re: Convert String to Number
« Reply #3 on: March 25, 2022, 07:20:03 PM »
Ok.

No it doesn't return the actual substring but if you have the indexes then getting the substring is trivial.

Although, I think you may not want people to submit other people's code (even if they are only meant as utilities) because that's a little unfair to the original author and not really teaching you or anyone else anything if you just use their process. 
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

cmwade77

  • Swamp Rat
  • Posts: 1361
Re: Convert String to Number
« Reply #4 on: March 25, 2022, 07:57:22 PM »
Although, I think you may not want people to submit other people's code (even if they are only meant as utilities) because that's a little unfair to the original author and not really teaching you or anyone else anything if you just use their process.
I think you may be right there, I will modify it when I have the chance.
« Last Edit: March 28, 2022, 11:31:04 AM by cmwade77 »