TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Red Nova on February 22, 2019, 08:32:38 AM

Title: Extract list of reals from string
Post by: Red Nova on February 22, 2019, 08:32:38 AM
Hi.
I am searching for a function to extract separate reals from a string. Does anyone have one? :)

Say from this:
"H=7.25\",W=2\""
I need to receive this:
(7.25 2)

Thank you
Title: Re: Extract separate list of reals from string
Post by: MP on February 22, 2019, 09:23:01 AM
Read up on functions: vl-string-translate, read, numberp, vl-remove-if-not ...
Title: Re: Extract list of reals from string
Post by: Red Nova on February 22, 2019, 09:53:10 AM
Just found it.
Lee Mac had it solved.
Thank you :)

http://www.lee-mac.com/parsenumbers.html
Title: Re: Extract list of reals from string
Post by: MP on February 22, 2019, 10:12:36 AM
This works too, although it is very specific to your need:

Code: [Select]
(defun foo ( text )
    (vl-remove-if-not 'numberp (read (vl-string-translate "=,\"" "   " (strcat "(" text ")"))))
)

(foo "H=7.25\",W=2\"")

>>  (7.25 2)


Cheers.
Title: Re: Extract list of reals from string
Post by: Lee Mac on February 22, 2019, 12:12:41 PM
Just found it.
Lee Mac had it solved.
Thank you :)

http://www.lee-mac.com/parsenumbers.html

 :-)
Title: Re: Extract list of reals from string
Post by: Grrr1337 on February 22, 2019, 06:15:42 PM
BTW maybe a technique like this would be more suitable -

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2. _$ (ini->aL "H=7.25\",W=2\",L=3.4")
  3. >> ((H . "7.25\"") (W . "2\"") (L . "3.4"))
  4. |;
  5. (defun ini->aL ( s )
  6.   (setq s (mapcar 'list (vl-string->list s)))
  7.   (foreach pair
  8.     '(
  9.       ((92 34) (34))
  10.       ((32 46 32 34) (61))
  11.       ((34 41 40) (44))
  12.       ((0) (32))
  13.       ((0) (10))
  14.     )
  15.     (setq s (apply 'subst (append pair (list s))))
  16.   )
  17.   (read  (strcat "((" (vl-list->string (apply 'append s))   "\"))"))
  18. )

including this sub, to strip the \" (inches) :
Code - Auto/Visual Lisp: [Select]
  1. (defun stripc ( c s )
  2.     (setq s (vl-string-subst "" c s))
  3.   )
  4.   s
  5. )

So then:
Code - Auto/Visual Lisp: [Select]
  1. _$ (ini->aL (stripc "\"" "H=7.25\",W=2\",L=3.4"))
  2. >> ((H . "7.25") (W . "2") (L . "3.4"))