Author Topic: Get list of string from string?  (Read 1135 times)

0 Members and 1 Guest are viewing this topic.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Get list of string from string?
« 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?

Code: [Select]

;; 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?
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016

David Bethel

  • Swamp Rat
  • Posts: 641
Get list of string from string?
« Reply #1 on: May 03, 2005, 08:32:25 AM »
Here's an old one that creates a list from a string and allows for STR or NUMBERP atoms.  -David

Code: [Select]
;|;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))

R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10369
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17490
Get list of string from string?
« Reply #3 on: May 03, 2005, 11:41:45 AM »
Quick thinking outside the box ...

Code: [Select]
(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)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox