TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: TimSpangler on May 03, 2005, 08:25:50 AM
-
I have created the following code that does what I want it to do but, can it be done better or faster?
;; Create filters from layer names
;; Sub to create a lists from a delimited string i.e. "a-anno-test" = ("a" "anno" "test")
(defun CREATE_FILTERS (String Delim / StringCount Counter ChurChar Sting1)
(setq Sting1 "")
(setq List1 '())
(setq StringCount (1+ (strlen String)))
(setq Counter 1)
(repeat StringCount
(setq ChurChar(substr String Counter 1))
(if (or(= ChurChar "")(= ChurChar Delim))
(progn
(setq List1 (cons Sting1 List1))
(setq Sting1 "")
)
(setq Sting1 (strcat ChurChar Sting1))
)
(setq Counter (1+ Counter))
)
(setq List1 (reverse(mapcar '(lambda (x)(REV_STRING x))List1)))
)
(defun REV_STRING (String / ChurChar Counter NewString StringCount)
(setq NewString "")
(setq StringCount (strlen String))
(setq Counter 1)
(repeat StringCount
(setq ChurChar (substr String Counter 1))
(setq NewString (strcat ChurChar NewString))
(setq Counter (1+ Counter))
)
NewString
)
Anyone willing to give it a go?
Edited:
I am also looking at changing the code to look for certian delimeters, then create the list of string based on certian delimeters.
Q:
What would you consider delimeters?
-
Here's an old one that creates a list from a string and allows for STR or NUMBERP atoms. -David
;|;SUB -> CONVERT DELIMITED STRING TO LIST (NUMERIC or STRING ATOMS)
;;;ARG -> Single Delimiter Character
String To Convert
Flag T=Numeric nil=String
;;;RET -> List or nil
;;;Error checking -> NONE what so ever |;
(defun ds2l (d s f / sl f sv dlist slist)
(setq sl (strlen s)
f 0)
(setq dlist (list 0))
(while (<= (setq f (1+ f)) sl)
(if (= d (substr s f 1))
(setq dlist (cons f dlist))))
(setq dlist (cons (1+ sl) dlist))
(setq dlist (reverse dlist))
(repeat (1- (length dlist))
(setq sv (substr s (1+ (nth 0 dlist))
(- (nth 1 dlist) (nth 0 dlist) 1)))
(if f
(setq slist (cons (atof sv) slist))
(setq slist (cons sv slist)))
(setq dlist (cdr dlist)))
(reverse slist))
-
Nice one David...
Other phasers
http://www.theswamp.org/phpBB2/viewtopic.php?t=3683&highlight=parselst
http://www.theswamp.org/phpBB2/viewtopic.php?t=3710&highlight=parselst
-
Quick thinking outside the box ...
(defun ToList ( string delim / value convert main )
;; delim is a one character string like
;; a comma "," or a pipe "|" etc.
(defun value ( x / result )
(vl-catch-all-apply
'(lambda ()
(vl-some
'(lambda (units) (setq result (distof x units)))
(list (getvar "lunits") 4 5 2 1)
)
)
)
(if result
(if (vl-string-position 46 x)
result
(fix result)
)
)
)
(defun convert ( lst / string )
(cond
( (value
(setq string
(vl-list->string lst)
)
)
)
( string )
)
)
(defun main ( string delim / item lst )
(setq delim (ascii delim))
(foreach code (reverse (vl-string->list string))
(cond
( (eq delim code)
(if item
(setq
lst (cons (convert item) lst)
item nil
)
)
)
( (setq item
(cons code item)
)
)
)
)
(if item
(cons (convert item) lst)
lst
)
)
(main string delim)
)
(ToList "abc,123,def,456.7" ",") => ("abc" 123 "def" 456.7)