Author Topic: -={ Challenge }=- Multi-Delimiter String Parsing  (Read 7288 times)

0 Members and 1 Guest are viewing this topic.

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #15 on: July 26, 2010, 06:43:19 AM »
my code, based on CAB and alanjt 's codes on one delim member

First is to subst all the other delims in the string to the first member of delim

Code: [Select]
;;;Thanks to alanjt and CAB's codes at : http://www.theswamp.org/index.php?topic=32845.0
;;;add a little by qjchen
(defun q:str:delim(str delimlst / l inc)
 (foreach x (cdr delimlst)
   (while (vl-string-search x str)
    (setq str (vl-string-subst (car delimlst) x str))))
 (while (setq inc (vl-string-search (car delimlst) str))
    (setq l (cons (substr str 1 inc) l)
          str (substr str (+ (strlen (car delimlst)) inc 1)))
 )
 (vl-remove "" (reverse (cons str l)))
)

(q:str:delim ";;;...This;is.a,Test;abStringab" '("ab" ";" "." ","))

To Daniel, when use (MultiParse ";;;...This;is.a,Test;abStringab" '("ab" ";" "." ","))
=> ("" "" "" "" "" "" "This" "is" "" "" "Test" "" "bString" "b")
« Last Edit: July 26, 2010, 08:01:30 AM by qjchen »
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

pkohut

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #16 on: July 26, 2010, 06:48:52 AM »
The dog ate my homework.    :roll:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #17 on: July 26, 2010, 08:33:07 AM »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8661
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #18 on: July 26, 2010, 08:36:26 AM »
To Daniel, when use (MultiParse ";;;...This;is.a,Test;abStringab" '("ab" ";" "." ","))
=> ("" "" "" "" "" "" "This" "is" "" "" "Test" "" "bString" "b")

 :-o sorry, I am very new at lisp  :laugh:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #19 on: July 26, 2010, 08:40:02 AM »
To Daniel, when use (MultiParse ";;;...This;is.a,Test;abStringab" '("ab" ";" "." ","))
=> ("" "" "" "" "" "" "This" "is" "" "" "Test" "" "bString" "b")

 :-o sorry, I am very new at lisp  :laugh:


It gets easier after 5 or 6 thousand lines of code ...


... oh, wait, you know that from one app don't you ??  ;-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8661
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #20 on: July 26, 2010, 08:43:44 AM »
ok hows this one?

Code: [Select]
(defun MultiParse (string delims / char cnt flag len lout s tdlim tstr)
  (setq tstr ""
        tdlim ""
        cnt 0
        len (strlen string)
        flag nil
        lout '()
  )
 
  (foreach s  delims
    (setq tdlim (strcat s tdlim)))
   
  (while (<= cnt len)
    (setq char (substr string (1+ cnt) 1))
    (if (vl-string-search   char tdlim)
      (progn
        (if flag
          (progn
            (setq lout (append lout (list tstr))
                  tstr ""
                  flag nil
            )
          )
        )
      )
      (progn
        (setq tstr (strcat tstr char)
              flag t
        )
      )
    )
    (setq cnt (1+ cnt))
  )
  lout
)

(MultiParse ";;;;;;;;;;;;;This;;;is.a,Test;String hi;,,,yes;;." '(";" "." ","))
(MultiParse "This&%is,5%" '("," "&%"))

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #21 on: July 26, 2010, 09:00:16 AM »
Code: [Select]
(defun Parse-AJT (s dLst / i l)
  (while (setq i (vl-remove nil
                            (mapcar
                              (function (lambda (x) (vl-string-search (strcase x) (strcase s))))
                              dLst
                            )
                 )
         )
    (setq l (cons (substr s 1 (setq i (apply (function min) i))) l)
          s (substr s (+ 2 i))
    )
  )
  (vl-remove "" (reverse (cons s l)))
)


Result:
Code: [Select]
(parse-ajt "1,2,3,4,5,6.7.8.9.10" (list "," "."))
("1" "2" "3" "4" "5" "6" "7" "8" "9" "10")
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8661
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #22 on: July 26, 2010, 09:04:16 AM »
Awseome, but fails here  (Parse-AJT "This&%is,5%" '("," "&%"))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #23 on: July 26, 2010, 09:08:22 AM »

(parse-ajt "1,2,3,4,5,6.7.8.9.10" (list  "," "5" "10" "."))

;;=>> ("1" "2" "3" "4" "6" "7" "8" "9" "0")

oops, Dainiel beat me again.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #24 on: July 26, 2010, 09:16:02 AM »
Oops, I forgot to account for strings being longer than one character.  :ugly:
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

hermanm

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #25 on: July 26, 2010, 09:32:39 AM »
So far:
Code: [Select]
Command: (LM:STRINGPARSERM "This\nis a;; test\n" '("\n" " " ";"))
("This" "is" "a" "test")

Command: (LM:STRINGPARSERM "This\nis a;; test\n" '("\n" " " ";;"))
("This" "is" "a" "test")

Command: (mparser '("\n" " " ";") "This\nis a;; test\n")
("This" "is" "a" "; test" "")

Command: (mparser '("\n" " " ";;") "This\nis a;; test\n")
("This" "is" "a" "test" "")

Command: (multiparse "This\nis a;; test\n" '("\n" " " ";"))
("This" "is" "a" "" "" "test")

Command: (multiparse "This\nis a;; test\n" '("\n" " " ";;"))
("This\nis" "a" "" "" "test\n")


hermanm

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #26 on: July 26, 2010, 09:35:32 AM »
Code: [Select]
Command: (parse-ajt "This\nis a;; test\n" '("\n" " " ";"))
("This" "is" "a" "test")

Command: (parse-ajt "This\nis a;; test\n" '("\n" " " ";;"))
("This" "is" "a" ";" "test")

hermanm

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #27 on: July 26, 2010, 09:40:15 AM »
and Dan's update:
Code: [Select]

Command: (multiparse "This\nis a;; test\n" '("\n" " " ";"))
("This" "is" "a" "test")

Command: (multiparse "This\nis a;; test\n" '("\n" " " ";;"))
("This" "is" "a" "test")

Sorry I don't have time to play :( :(

hermanm

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #28 on: July 26, 2010, 09:44:08 AM »


Code: [Select]
Command: (Q:STR:DELIM "This\nis a;; test\n" '("\n" " " ";"))
("This" "is" "a" "test")

Command: (Q:STR:DELIM "This\nis a;; test\n" '("\n" " " ";;"))
("This" "is" "a" "test")

(not claiming the test strings to be anything special, just what popped into my head)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #29 on: July 26, 2010, 10:07:12 AM »
Code: [Select]
(defun Parse-AJT2 (s dLst / i n l)
  (while (setq i (vl-remove nil
                            (mapcar
                              (function (lambda (x / v)
                                          (if (setq v (vl-string-search (strcase x) (strcase s)))
                                            (cons v (1- (strlen x)))
                                          )
                                        )
                              )
                              dLst
                            )
                 )
         )
    (setq l (cons (substr s 1 (setq n (caar (vl-sort i '(lambda (a b) (< (car a) (car b))))))) l)
          s (substr s (+ 2 (cdr (assoc n i)) n))
    )
  )
  (vl-remove "" (reverse (cons s l)))
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox