Author Topic: Extract list of reals from string  (Read 2331 times)

0 Members and 1 Guest are viewing this topic.

Red Nova

  • Newt
  • Posts: 69
Extract list of reals from string
« 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
« Last Edit: February 22, 2019, 09:39:41 AM by Red Nova »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Extract separate list of reals from string
« Reply #1 on: February 22, 2019, 09:23:01 AM »
Read up on functions: vl-string-translate, read, numberp, vl-remove-if-not ...
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Red Nova

  • Newt
  • Posts: 69
Re: Extract list of reals from string
« Reply #2 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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Extract list of reals from string
« Reply #3 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Extract list of reals from string
« Reply #4 on: February 22, 2019, 12:12:41 PM »

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Extract list of reals from string
« Reply #5 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"))
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg