Author Topic: Please help Converting delimited strings to list  (Read 356 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1059
Please help Converting delimited strings to list
« on: June 18, 2019, 12:17:42 PM »
I'm having a difficulty understanding on how to convert strings to list.

The end product i would like a list like this
Code: [Select]
(("2019/06/18 08:07:33" "username" "Finished" "test1")
("2019/06/18 08:07:33" "username" "Finished" "test2")
("2019/06/18 08:07:33" "username" "Finished" "test3"))

Comments are stored dwgprops
Quote
2019/06/18 08:07:33   username   Finished   test1
2019/06/18 08:07:33   username   Finished   test2
2019/06/18 08:07:33   username   Finished   test3

I use a function retrieve the comments into a string.
Code: [Select]
Command: (getcomments)
"2019/06/18 08:07:33\tusername\tFinished\ttest1\r\n2019/06/18 08:07:33\tusername\tFinished\ttest2\r\n2019/06/18 08:07:33\tusername\tFinished\ttest3"

I'm confused on how to convert them into a list.
Code: [Select]
(defun getcomments( / acadObj acDocSumInfo )
  (vl-load-com)
(setq acadObj (vlax-get-acad-object))
(setq acDoc (vlax-get-property acadObj 'ActiveDocument))
(setq acDocSumInfo (vlax-get-property acDoc 'SummaryInfo))
;(setq dwgpropcomments (vlax-get-property acDocSumInfo 'Comments))
(vlax-get-property acDocSumInfo 'Comments)
  )

(defun foo( / pos )
(setq str (getcomments))
(while (vl-string-search "\r\n" str)
  (if (vl-string-search "\r\n" str)
    (setq pos (vl-string-search "\r\n" str))
    );end if
  (if (= nil (vl-string-search "\r\n" str))
    (princ (strcat "\n"str))
    (progn
    (princ (strcat "\n"(substr str 1 pos)))
    (setq str(substr str (+ pos 3) (strlen str)))
    )
    )
  );end while
  );end defun

(defun test(str / lst)
(while (vl-string-search "\t" str)
  (if (vl-string-search "\t" str)
    (progn
      (setq pos (vl-string-search "\t" str))
      (setq lst (append (list(substr str 1 pos)) lst))
      (setq str (substr str (+ 2 pos) (strlen str)))
      );end prog
    );end if
  );end while
  (setq lst (list(append (list str) lst)))
  (princ lst)
  )





kpblc

  • Bull Frog
  • Posts: 320
Re: Please help Converting delimited strings to list
« Reply #1 on: June 18, 2019, 01:40:29 PM »
My old function:
Code - Auto/Visual Lisp: [Select]
  1. (defun _kpblc-conv-string-to-list (string separator / i)
  2.                                   ;|
  3. *    Convert string to list
  4. *    Call parameters:
  5.   string      ; string to proceed
  6.   separator   ; symbol(s) to split string
  7. *    Call samples:
  8. (_kpblc-conv-string-to-list "1;2;3;4;5;6" ";")  ;-> '(1 2 3 4 5 6)
  9. (_kpblc-conv-string-to-list "1;2" ";")          ;-> '(1 2)
  10. (_kpblc-conv-string-to-list "1,2" ",")          ;-> '(1 2)
  11. (_kpblc-conv-string-to-list "1.2" ".")          ;-> '(1 2)
  12. |;
  13.   (cond ((= string "") nil)
  14.         ((vl-string-search separator string)
  15.          ((lambda (/ pos res)
  16.             (while (setq pos (vl-string-search separator string))
  17.               (setq res    (cons (substr string 1 pos) res)
  18.                     string (substr string (+ (strlen separator) 1 pos))
  19.                     ) ;_ end of setq
  20.               ) ;_ end of while
  21.             (reverse (cons string res))
  22.             ) ;_ end of lambda
  23.           )
  24.          )
  25.         ((and (not (member separator '("`" "#" "@" "." "*" "?" "~" "[" "]" "-" ",")))
  26.               (wcmatch (strcase string) (strcat "*" (strcase separator) "*"))
  27.               ) ;_ end of and
  28.          ((lambda (/ pos res _str prev)
  29.             (setq pos  1
  30.                   prev 1
  31.                   _str (substr string pos)
  32.                   ) ;_ end of setq
  33.             (while (<= pos (1+ (- (strlen string) (strlen separator))))
  34.               (if (wcmatch (strcase (substr string pos (strlen separator))) (strcase separator))
  35.                 (setq res    (cons (substr string 1 (1- pos)) res)
  36.                       string (substr string (+ (strlen separator) pos))
  37.                       pos    0
  38.                       ) ;_ end of setq
  39.                 ) ;_ end of if
  40.               (setq pos (1+ pos))
  41.               ) ;_ end of while
  42.             (if (< (strlen string) (strlen separator))
  43.               (setq res (cons string res))
  44.               ) ;_ end of if
  45.             (if (or (not res) (= _str string))
  46.               (setq res (list string))
  47.               (reverse res)
  48.               ) ;_ end of if
  49.             ) ;_ end of lambda
  50.           )
  51.          )
  52.         (t (list string))
  53.         ) ;_ end of cond
  54.   ) ;_ end of defun
Sorry for my English.

kpblc

  • Bull Frog
  • Posts: 320
Re: Please help Converting delimited strings to list
« Reply #2 on: June 18, 2019, 01:42:35 PM »
At your situation call it like:
Code - Auto/Visual Lisp: [Select]
  1. (mapcar (function (lambda(a) (_kpblc-conv-string-to-list a "\t"))) (_kpblc-conv-string-to-list "2019/06/18 08:07:33\tusername\tFinished\ttest1\r\n2019/06/18 08:07:33\tusername\tFinished\ttest2\r\n2019/06/18 08:07:33\tusername\tFinished\ttest3" "\r\n"))
Sorry for my English.

dubb

  • Swamp Rat
  • Posts: 1059
Re: Please help Converting delimited strings to list
« Reply #3 on: June 18, 2019, 02:08:36 PM »
Beautiful. This works great.
My old function:
Code - Auto/Visual Lisp: [Select]
  1. (defun _kpblc-conv-string-to-list (string separator / i)
  2.                                   ;|
  3. *    Convert string to list
  4. *    Call parameters:
  5.   string      ; string to proceed
  6.   separator   ; symbol(s) to split string
  7. *    Call samples:
  8. (_kpblc-conv-string-to-list "1;2;3;4;5;6" ";")  ;-> '(1 2 3 4 5 6)
  9. (_kpblc-conv-string-to-list "1;2" ";")          ;-> '(1 2)
  10. (_kpblc-conv-string-to-list "1,2" ",")          ;-> '(1 2)
  11. (_kpblc-conv-string-to-list "1.2" ".")          ;-> '(1 2)
  12. |;
  13.   (cond ((= string "") nil)
  14.         ((vl-string-search separator string)
  15.          ((lambda (/ pos res)
  16.             (while (setq pos (vl-string-search separator string))
  17.               (setq res    (cons (substr string 1 pos) res)
  18.                     string (substr string (+ (strlen separator) 1 pos))
  19.                     ) ;_ end of setq
  20.               ) ;_ end of while
  21.             (reverse (cons string res))
  22.             ) ;_ end of lambda
  23.           )
  24.          )
  25.         ((and (not (member separator '("`" "#" "@" "." "*" "?" "~" "[" "]" "-" ",")))
  26.               (wcmatch (strcase string) (strcat "*" (strcase separator) "*"))
  27.               ) ;_ end of and
  28.          ((lambda (/ pos res _str prev)
  29.             (setq pos  1
  30.                   prev 1
  31.                   _str (substr string pos)
  32.                   ) ;_ end of setq
  33.             (while (<= pos (1+ (- (strlen string) (strlen separator))))
  34.               (if (wcmatch (strcase (substr string pos (strlen separator))) (strcase separator))
  35.                 (setq res    (cons (substr string 1 (1- pos)) res)
  36.                       string (substr string (+ (strlen separator) pos))
  37.                       pos    0
  38.                       ) ;_ end of setq
  39.                 ) ;_ end of if
  40.               (setq pos (1+ pos))
  41.               ) ;_ end of while
  42.             (if (< (strlen string) (strlen separator))
  43.               (setq res (cons string res))
  44.               ) ;_ end of if
  45.             (if (or (not res) (= _str string))
  46.               (setq res (list string))
  47.               (reverse res)
  48.               ) ;_ end of if
  49.             ) ;_ end of lambda
  50.           )
  51.          )
  52.         (t (list string))
  53.         ) ;_ end of cond
  54.   ) ;_ end of defun

ronjonp

  • Needs a day job
  • Posts: 7027
Re: Please help Converting delimited strings to list
« Reply #4 on: June 18, 2019, 03:24:10 PM »
Another using Lee's 'LM:str->lst' ( http://www.lee-mac.com/stringtolist.html ) function:

Code - Auto/Visual Lisp: [Select]
  1.   '(lambda (x) (lm:str->lst x "\t"))
  2.   (lm:str->lst
  3.     "2019/06/18 08:07:33\tusername\tFinished\ttest1\r\n2019/06/18 08:07:33\tusername\tFinished\ttest2\r\n2019/06/18 08:07:33\tusername\tFinished\ttest3"
  4.     "\r\n"
  5.   )
  6. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

dubb

  • Swamp Rat
  • Posts: 1059
Re: Please help Converting delimited strings to list
« Reply #5 on: June 19, 2019, 10:54:37 AM »
Lee's version uses less code. Interesting.
Another using Lee's 'LM:str->lst' ( http://www.lee-mac.com/stringtolist.html ) function:

Code - Auto/Visual Lisp: [Select]
  1.   '(lambda (x) (lm:str->lst x "\t"))
  2.   (lm:str->lst
  3.     "2019/06/18 08:07:33\tusername\tFinished\ttest1\r\n2019/06/18 08:07:33\tusername\tFinished\ttest2\r\n2019/06/18 08:07:33\tusername\tFinished\ttest3"
  4.     "\r\n"
  5.   )
  6. )

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Please help Converting delimited strings to list
« Reply #6 on: June 20, 2019, 08:22:00 AM »
Old topichttps://www.google.com/search?hl=en&as_q=parse+string&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Fwww.theswamp.org&as_occt=any&safe=images&as_filetype=&as_rights=
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.