View Full Version : Extracting real numbers from text
MWKINCAID1
2007-03-06, 06:40 PM
Hey guys, I’m working on a lisp routine to extract the numbers out of a string of text and then using lisp to put them back together in a new order.
These are the 2 strings of text:
"PVI STA = 14+29.57"
"PVI ELEV = 12.30"
Extracting those numbers and setting them as variables is the last piece to the puzzle, so any help or direction would be greatly appreciated.
mk
kennet.sjoberg
2007-03-06, 07:08 PM
Here is something to start with
; (setq sym expr [sym expr]...) Sets the value of a symbol or symbols to associated expressions
(setq StrA "PVI STA = 14+29.57" )
(setq StrB "PVI ELEV = 12.30" )
; (substr string start [length]) Returns a substring of a string
(setq StrA1 (substr StrA 11 2 )) ; --> "14"
(setq StrA2 (substr StrA 14 )) ; --> "29.57"
(setq StrB1 (substr StrB 12 )) ; --> "12.30"
; (atoi string) Converts a string into an integer
; (atof string) Converts a string into a real number
(setq NumA1 (atoi StrA1 )) ; --> 14
(setq NumA2 (atof StrA2 )) ; --> 29.57
(setq NumB1 (atof StrB1 )) ; --> 12.3
: ) Happy Computing !
kennet
MWKINCAID1
2007-03-06, 07:30 PM
Thank you very much for the assistance, the additional layer of complexity and the reason i haven't selected substr as my way of extracting the number is because some time's it's 14, and sometimes it's 152.
The number before the "+" can vary from 1-4 characters. as well as the number after the "elev ="
ex 1.
"PVI STA = 14+29.57"
"PVI ELEV = 12.30"
ex2.
"PVI STA = 138+25.47"
"PVI ELEV = 132.30"
Thanks again, mk
kennet.sjoberg
2007-03-06, 07:46 PM
OK, you find the positon with this code, to use in the (substr string start [length])
(vl-string-position char-codestr [start-pos [from-end-p]]) Looks for a character with the specified ASCII code in a string
(vl-string-position (ascii "+" ) "huhfdhodahf+jdsajdj" ) -->11
: ) Happy Computing !
kennet
BTW
(setq StrB1 (substr StrB 12 )) ; --> "12.30"
goes from startpt 12 to end of string
MWKINCAID1
2007-03-07, 04:25 AM
Okay guys,
(defun C:VPT ()
;(princ "- Takes the LDD generated Vertical Alignment text and formats it according to QEI Standards")
;=== sets new system variable
(setvar "cmdecho" 1)
;=== places who created the command
;(princ "\nCreated by MWK for QEI on 29 Jan 07")
(setq tx1 "PVI STA = 14+29.57" )
(setq tx2 "PVI ELEV = 12.30" )
(setq tx3 (substr tx1 11 2 ))
(setq tx4 (substr tx1 14 ))
(setq tx5 (substr tx2 12 ))
(setq tx6 (atoi tx3 ))
(setq tx7 (atof tx4 ))
(setq tx8 (atof tx5 ))
(setq tx9 " PVI")
(setq tx10 "STA ")
(setq tx11 " = ")
(setq tx12 "+")
(setq tx13 "ELEV")
(setq tx14 (strcat tx10 tx3 tx12 tx4 tx9))
(setq tx15 (strcat tx13 tx11 tx5 tx9))
(COMMAND ".-MTEXT" "end" pause "rotation""90" "justify""mr" "@" tx14 tx15 ""))
Allrighty, i think i got this right so far. wanted to share my progressional success
Thanks again kennet,
p.s. the atof is comming, that's the next step
peter
2007-03-07, 02:19 PM
This should do it
(defun ConvertToNumber (strStation / lstStation)
(setq lstStation (vl-string->list strStation))
(if (member 61 lstStation)
(setq lstStation (cdr (member 61 lstStation)))
)
(if (member 43 lstStation)
(setq lstStation (vl-remove 43 lstStation))
)
(distof (vl-list->string lstStation))
)
MWKINCAID1
2007-03-08, 06:07 PM
Okay, on to my next challange. I have X number of text entities on a "vertical text" layer, there all regular text, rotated 90deg, and there paired. for every "PVI STATION ##+##.##" there is a "PVI ELEV = ##.##"
I'm trying to automaticaly build 2 lists and filter them via the X axis. One list for the Station text string, and One list for the ELEV text string.
After that then i'm going down the list, extracting the numerical value out of it, and out putting it in the previously shown lisp. I've been trying to use ssget and entget, but since i'm kind of new to this, it seems a little over my head at the moment. any help would be really great. thanks again for all of your support.
MWKINCAID1
2007-03-09, 02:29 AM
i've jumped to a new thread; you can search for list/loop help,.. thanks again
[ Moderator Action = ON ] For new thread, see here [ Create two lists and process the data ] [ Moderator Action = OFF ]
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.