<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Separate string into lists in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8167660#M101681</link>
    <description>&lt;P&gt;Yes, I would definitely go for recursive as well now.&lt;/P&gt;&lt;P&gt;But I wrote the code many many years ago, before even considering recursive programming. Things have changed however.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I've rewritten my old code now to be more fool proof and recursive. As an added bonus the text delimiter can now be any string and doesn't need to be only one character, although for the purpose of splitting strings from a CSV file it doesn't have any added value.&lt;/P&gt;&lt;PRE&gt;(defun String2List (S2L_String S2L_Delimiter / S2L_Found)
   (cond
      (
         (or                        
            (/= (type S2L_String)    'STR)
            (/= (type S2L_Delimiter) 'STR)  
         )
         nil
      )
      (
         (or
            (= S2L_Delimiter "")
            (not (setq S2L_Found (vl-string-search S2L_Delimiter S2L_String)))
         )
         (list S2L_String)
      )
      (
         T
         (cons (substr S2L_String 1 S2L_Found) (String2List (substr S2L_String (+ S2L_Found (1+ (strlen S2L_Delimiter)))) S2L_Delimiter))
      )
   )
)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 31 Jul 2018 13:57:55 GMT</pubDate>
    <dc:creator>DannyNL</dc:creator>
    <dc:date>2018-07-31T13:57:55Z</dc:date>
    <item>
      <title>Separate string into lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8164523#M101678</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;I've a CSV file whose each line is a list of attributes I have to insert in a determinate block. This is one example:&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Value 1&lt;/TD&gt;&lt;TD&gt;Value 2&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Value 3&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Value 1&lt;/TD&gt;&lt;TD&gt;Value 2&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Value 3&lt;/TD&gt;&lt;TD&gt;Value 4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Name&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Value 1&lt;/TD&gt;&lt;TD&gt;Value 2&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me explain: Block's type is given at the first column and the remainder columns are the attribute values I've to read. Not every type of block have the same number of attributes...&lt;/P&gt;&lt;P&gt;I've done something like that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;setq f (open "C:\\Users\\User\\Documents\\MyCSV.csv" "r"))
(while (setq txtline(read-line f))
  (princ txtline)
  (princ "\n")
)&lt;/PRE&gt;&lt;P&gt;And I get txtline as a String similar to: Name;Value 1;Value 2;Value 3 and so on. Now, What I need is split this string and convert it into a list so my final list must be equal to this (for the first row): (list "Name" "Value 1" "Value 2" "Value 3")&lt;/P&gt;&lt;P&gt;I don't know how to do this. I belive it's a simple task but I can't do it&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 11:36:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8164523#M101678</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-07-30T11:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: Separate string into lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8164611#M101679</link>
      <description>&lt;P&gt;Think there are lot of examples already on how to do this, but this is my old piece of code that I use to split strings into lists.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;(defun String2List (S2L_String S2L_Delimiter / S2L_Return S2L_End S2L_EndPos S2L_Item)
  (setq S2L_End nil)
  (while
     (and
        (setq S2L_EndPos (vl-string-search S2L_Delimiter S2L_String))
        (not S2L_End)
     )
     (if
        S2L_EndPos
        (progn
	   (setq S2L_Item (substr S2L_String 1 S2L_EndPos))
	   (setq S2L_Return (append S2L_Return (list S2L_Item)))
	   (setq S2L_String (substr S2L_String (+ S2L_EndPos 2)))
        )
        (setq S2L_End T)
     )   
  )
  (if
     (/= S2L_String "")
     (setq S2L_Return (append S2L_Return (list S2L_String)))
  )
  S2L_Return  
)&lt;/PRE&gt;&lt;P&gt;So in your case it would be the code below to split the string into the required list.&lt;/P&gt;&lt;PRE&gt;(String2List txtline ";")&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 12:16:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8164611#M101679</guid>
      <dc:creator>DannyNL</dc:creator>
      <dc:date>2018-07-30T12:16:12Z</dc:date>
    </item>
    <item>
      <title>Re: Separate string into lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8166815#M101680</link>
      <description>&lt;P&gt;Here's a recursive splitting function:&lt;/P&gt;&lt;PRE&gt;(defun split-at (char str / pos  )
  ;; Splits string to a list of strings at each char occurrence.
  ;; Separators omitted from the result.
  ;; char as an one-character string.
  (setq pos (vl-string-position (ascii char) str))
  (cond
    ((or (null char) (null str)) nil)
    ((null pos) (list str))
    (T (cons (substr str 1 pos)
             (split-at char (substr str (+ 2 pos)))))))&lt;/PRE&gt;&lt;P&gt;--&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 07:31:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8166815#M101680</guid>
      <dc:creator>martti.halminen</dc:creator>
      <dc:date>2018-07-31T07:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Separate string into lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8167660#M101681</link>
      <description>&lt;P&gt;Yes, I would definitely go for recursive as well now.&lt;/P&gt;&lt;P&gt;But I wrote the code many many years ago, before even considering recursive programming. Things have changed however.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I've rewritten my old code now to be more fool proof and recursive. As an added bonus the text delimiter can now be any string and doesn't need to be only one character, although for the purpose of splitting strings from a CSV file it doesn't have any added value.&lt;/P&gt;&lt;PRE&gt;(defun String2List (S2L_String S2L_Delimiter / S2L_Found)
   (cond
      (
         (or                        
            (/= (type S2L_String)    'STR)
            (/= (type S2L_Delimiter) 'STR)  
         )
         nil
      )
      (
         (or
            (= S2L_Delimiter "")
            (not (setq S2L_Found (vl-string-search S2L_Delimiter S2L_String)))
         )
         (list S2L_String)
      )
      (
         T
         (cons (substr S2L_String 1 S2L_Found) (String2List (substr S2L_String (+ S2L_Found (1+ (strlen S2L_Delimiter)))) S2L_Delimiter))
      )
   )
)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jul 2018 13:57:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8167660#M101681</guid>
      <dc:creator>DannyNL</dc:creator>
      <dc:date>2018-07-31T13:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: Separate string into lists</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8168181#M101682</link>
      <description>&lt;P&gt;You might like this version...&lt;/P&gt;&lt;PRE&gt;;;-------------------------------------------------------------
;; This function takes a delimited string, and returns a list:
;;------------------------------
;;Don: (02-09-02)
;;
;;First let me point out that the one you call JohnU is actually
;;Marc'Antonio's from November.
;;
;;As Marc'Antonio sagely reminded us, setting string symbols is slower than
;;setting integers.
;;
;;Your offering is the almost the same a Luis' except that yours doesn't
;;provide for validating the input or for multiple-character delimiters.
;;
;;While Marc'Antonio's method is the fastest, I've grown a liking to the
;;multiple-character delimiter, which his will not handle.
;;
;;Taking the best from Luis and Marc'Antonio, plus acknowledgments to Eric
;;Schneider, I now offer the following...
(defun @cv_str2list (str pat / i j n lst)
  (cond
    ((/= (type str)(type pat) 'STR))
    ((= str pat)'(""))
    (T
      (setq i 0 n (strlen pat))
      (while (setq j (vl-string-search pat str i))
        (setq lst (cons (substr str (1+ i)(- j i)) lst)
              i (+ j n)
        )
      )
      (reverse (cons (substr str (1+ i)) lst))
    )
  )
)
;; It's not as fast as Marc'Antonio's, but pretty close.&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Jul 2018 16:36:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/separate-string-into-lists/m-p/8168181#M101682</guid>
      <dc:creator>john.uhden</dc:creator>
      <dc:date>2018-07-31T16:36:43Z</dc:date>
    </item>
  </channel>
</rss>

