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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
-={ Challenge }=- Multi-Delimiter String Parsing
« on: July 25, 2010, 10:36:35 AM »
The Challenge:

To separate elements of a string by a list of delimiters.

Example:

Code: [Select]
(MultiParse "This;is.a,Test;String" '(";" "." ","))

==>  ("This" "is" "a" "Test" "String")

Code: [Select]
(MultiParse "This&%is,5%" '("," "&%"))

==>  ("This" "is" "5%")

Code: [Select]
(MultiParse "&This,is,a&Test," '("&" ","))

==> ("This" "is" "a" "Test")

Enjoy!

Lee

« Last Edit: July 25, 2010, 11:45:04 AM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #1 on: July 25, 2010, 11:26:53 AM »
Here is a quickie. 8-)
Code: [Select]
(defun sparserm (str delim / ptr lst)
  (while (setq ptr (vl-remove 'nil (mapcar (function (lambda(x) (vl-string-search x str))) delim)))
    (setq ptr (apply 'min ptr))
    (setq lst (cons (substr str 1 ptr) lst))
    (setq str (substr str (+ ptr 2)))
  )
  (reverse(cons str lst))
)
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.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #2 on: July 25, 2010, 11:39:01 AM »
Nice one Alan  8-)

How about for delims of length > 1  :evil:

Code: [Select]
(sparserm "This&%is,5%" '("," "&%"))

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #3 on: July 25, 2010, 11:42:46 AM »
My first entry:

Code: [Select]
(defun LM:StringParserM ( str del / foo l )

  (defun foo ( str del )
    (if (setq pos (vl-string-search del str))
      (vl-remove ""
        (cons (substr str 1 pos)
          (foo
            (substr str (+ pos 1 (strlen del))) del
          )
        )
      )
      (list str)
    )
  )

  (setq l (foo str (car del)))
 
  (foreach x (cdr del)
    (setq l
      (apply (function append)
        (mapcar (function (lambda ( y ) (foo y x))) l)
      )
    )
  )
  l
)
« Last Edit: July 25, 2010, 11:47:24 AM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #4 on: July 25, 2010, 12:06:33 PM »
How about for delims of length > 1  :evil:


Was that in the specifications?  :evil:
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.

hermanm

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #5 on: July 25, 2010, 01:14:01 PM »
Nice one, Lee.:)
Code: [Select]
Command: (LM:StringParserM "This;is.a,Test;\String" '(";" "." "," "\\"))
("This" "is" "a" "Test" "String")

Code: [Select]
Command: (LM:StringParserM "This\nis a test\n     " '(" " "\n"))
("This" "is" "a" "test")

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #6 on: July 25, 2010, 02:45:33 PM »
How about for delims of length > 1  :evil:


Was that in the specifications?  :evil:

I hadn't included an example, but it was my intention  :wink:

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #7 on: July 25, 2010, 02:47:03 PM »
Nice one, Lee.:)
Code: [Select]
Command: (LM:StringParserM "This;is.a,Test;\String" '(";" "." "," "\\"))
("This" "is" "a" "Test" "String")

Code: [Select]
Command: (LM:StringParserM "This\nis a test\n     " '(" " "\n"))
("This" "is" "a" "test")

Thanks Herman  :-)

LE3

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #8 on: July 25, 2010, 04:11:34 PM »
Code: [Select]
;;(mparser '(";" "." "," "\\") "This;is.a,Test;\String")
;;("This" "is" "a" "Test" "String")
;;(mparser '(";" "." ",") "This;is.a,Test;String")
;;("This" "is" "a" "Test" "String")
(defun mparser (pat str / pattern i j n lst)
  (setq p (car pat))
  (setq pattern (vl-remove p pat))
  (setq i 0)
  (repeat (length pattern)
    (setq str (vl-string-subst p (nth i pattern) str))
    (setq i (1+ i))
  )
  (cond
    ((/= (type str) (type p) 'STR))
    ((= str p) '(""))
    (T
     (setq i 0
  n (strlen p)
     )
     (while (setq j (vl-string-search p str i))
       (setq lst (cons (substr str (1+ i) (- j i)) lst)
    i (+ j n)
       )
     )
     (mapcar '(lambda (x) (vl-string-trim " " x))
    (reverse (cons (substr str (1+ i)) lst))
     )
    )
  )
)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #9 on: July 25, 2010, 05:17:24 PM »
Another,

Code: [Select]
(defun LM:StringParserM2 ( str del / pos x )
  ;; © Lee Mac 2010
  (if (setq pos
        (car
          (setq x
            (car
              (vl-sort
                (vl-remove nil
                  (mapcar
                    (function
                      (lambda ( d / p )
                        (if (setq p (vl-string-search d str))
                          (cons p (strlen d))
                        )
                      )
                    )
                    del
                  )
                )
                (function
                  (lambda ( a b ) (< (car a) (car b)))
                )
              )
            )
          )
        )
      )
    (vl-remove ""
      (cons (substr str 1 pos)
        (LM:StringParserM2
          (substr str (+ pos 1 (cdr x))) del
        )
      )
    )
    (list str)
  )
)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #10 on: July 25, 2010, 05:42:54 PM »
Perhaps a slightly more elegant version of my first one  :-)

Code: [Select]
(defun LM:StringParserM ( str del / foo l )

  (defun foo ( str del )
    (if (setq pos (vl-string-search del str))
      (vl-remove ""
        (cons (substr str 1 pos)
          (foo
            (substr str (+ pos 1 (strlen del))) del
          )
        )
      )
      (list str)
    )
  )

  (if (cdr del)
    (apply (function append)
      (mapcar (function (lambda ( x ) (LM:StringParserM x (cdr del))))
        (foo str (car del))
      )
    )
    (foo str (car del))
  )
)

LE3

  • Guest
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #11 on: July 25, 2010, 07:46:51 PM »
edited.-
Code: [Select]
;;parsermult uses a token character separator to replace
;;all the characters separators in the patterns list
;;will check if the token is not on the patterns or in the string
;;returns= a list of new strings or nil
;;usage:
;;(parsermult "this;is;;a,test\"\"" '(";" "," "\"") "|")
;;("this" "is" "a" "test")
(defun parsermult  (string patterns token / pattern pos lst)
  (if (and (not (vl-position token patterns))
   (not (vl-string-search token string)))
    (progn
      (while (setq pattern (car patterns))
(while (and pattern (vl-string-search pattern string))
  (setq string (vl-string-subst token pattern string)))
(setq patterns (cdr patterns)))
      (while (setq pos (vl-string-search token string))
(setq lst    (cons (substr string 1 pos) lst)
      string (substr string (+ pos 2))))
      (vl-remove "" (reverse lst)))))
« Last Edit: July 26, 2010, 12:08:51 AM by LE3 »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #12 on: July 26, 2010, 03:13:07 AM »
mine  :-D

Code: [Select]
(defun MultiParse (string delims / dlist item llout tmpl)
  (setq tmpl '() llout '()  dlist (vl-string->list delims))
  (foreach item (vl-string->list (vl-string-right-trim delims string))
    (if (not (vl-position item dlist))
      (setq tmpl (cons item tmpl))
      (progn
        (setq llout (append llout (list(vl-list->string(reverse tmpl)))) tmpl '())
      )
    )
  )
  (append llout (list(vl-list->string(reverse tmpl))))
)

(MultiParse "This;is.a,Test;String" ";.,")

Kerry

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

Daniel, I have one with similar functionality :

However I think the expectation was that the delimiters be a list of strings, each of which shall be removed from the parent string if existing.

... this should allow the the removal of character patterns like " ex" and "'s " from the parent.


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: 8691
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #14 on: July 26, 2010, 03:57:59 AM »
ah ok, maybe something like

Code: [Select]
(defun MultiParse (string delims / item llout s tdlim tmpl tstr)
  (setq tstr "" tdlim "")
  (repeat (length delims)
    (setq tstr (strcat " " tstr)))
  (foreach s  delims
    (setq tdlim (strcat s tdlim)))
  (setq tmpl '() llout '())
  (foreach item (vl-string->list (vl-string-right-trim " "(vl-string-translate tdlim tstr string)))
    (if (/= item 32)
      (setq tmpl (cons item tmpl))
      (progn
        (setq llout (append llout (list(vl-list->string(reverse tmpl)))) tmpl '())
      )
    )
  )
  (append llout (list(vl-list->string(reverse tmpl))))
)

(MultiParse "This;is.a,Test;String " '(";" "." ","))
« Last Edit: July 26, 2010, 04:01:11 AM by eAmbiguousOutput »

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: 8691
  • 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: 8691
  • 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: 8691
  • 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

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #30 on: July 26, 2010, 10:08:57 AM »
Code: [Select]
(defun Parse-AJT3 (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 (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 (+ 1 (cdr (assoc n i)) n))
    )
  )
  (vl-remove "" (reverse (cons s l)))
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #31 on: July 26, 2010, 10:11:57 AM »
.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #32 on: July 26, 2010, 10:26:34 AM »
qjchen, that's quite impressive.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #33 on: July 26, 2010, 10:50:30 AM »
Thank you Daniel for your test. I am not sure whether there are still bugs in my codes.

And thank you alanjt, my codes are mainly based on yours and CAB's codes.  I changed the "2" in your code  to "(strlen delim)" to be applicable for multi-char delim


http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #34 on: July 26, 2010, 10:59:15 AM »
i insist that a split function should work like this
Code: [Select]
(somesplit ";This;;is;a;test;" '(";"))
==>("" "This" "" "is" "a" "test" "")

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #35 on: July 26, 2010, 10:59:29 AM »
Thank you Daniel for your test. I am not sure whether there are still bugs in my codes.

And thank you alanjt, my codes are mainly based on yours and CAB's codes.  I changed the "2" in your code  to "(strlen delim)" to be applicable for multi-char delim



Well, I like it! :)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #36 on: July 26, 2010, 11:13:38 AM »
a bit faster but not enough

Code: [Select]
(defun MultiParse2 (string delims / char cnt flag len lout s tstr)
  (setq tstr ""
        tdlim ""
        cnt 0
        flag n
        lout '()
        delim (car delims)
  )

  (foreach s delims
    (setq tdlim (strcat s tdlim))
  )
  (repeat (strlen tdlim)
    (setq tstr (strcat delim tstr))
  )

  (setq string (vl-string-trim " " (vl-string-translate tdlim tstr string))
        tstr ""
        len (strlen string)
  )

  (while (<= cnt len)
    (setq cnt (1+ cnt))
    (setq char (substr string cnt 1))
    (if (= char delim)
      (progn
        (if flag
          (progn
            (setq lout (append lout (list tstr))
                  tstr ""
                  flag nil
            )
          )
        )
      )
      (progn
        (setq tstr (strcat tstr char)
              flag t
        )
      )
    )
  )
  lout
)


VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #37 on: July 26, 2010, 11:34:48 AM »
does anyone mind that mine doesn't work with "\"" ? :)
Code: [Select]
(defun msplit (str dList)
  (foreach d dList (setq str (vk_StringSubstPat "\" \"" d str nil)))
  (read (strcat "(\"" str "\")"))
)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #38 on: July 26, 2010, 03:31:04 PM »
Damn I wish I had more time to play around with this  :|

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #39 on: July 26, 2010, 04:01:44 PM »
Minor attempt at optimisation of Chen's

Code: [Select]
(defun LM:StringParserM3 ( str del / x y z l p )

  (setq x (car del) y (1+ (strlen x)))
 
  (while (setq del (cdr del))
    (while (vl-string-search (setq z (car del)) str)
      (setq str (vl-string-subst x z str))
    )
  )

  (while (setq p (vl-string-search x str))
    (setq l (cons (substr str 1 p) l) str (substr str (+ p y)))
  )

  (vl-remove "" (reverse (cons str l)))
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #40 on: July 27, 2010, 08:46:13 AM »
Code: [Select]
(defun f1 (s p / i l)
 (while (setq i (vl-string-search p s))
  (setq l (cons (substr s 1 i) l)
        s (substr s (+ 1 i (strlen p)))
  )
 )
 (reverse (cons s l))
)
(defun mpars (s d)
 (setq d1 ""
       d2 ""
       d3 '("\001")
 )
 (foreach p d
  (if (= (substr p 2) "")
   (setq d1 (strcat p d1)
         d2 (strcat "\001" d2)
   )
   (setq d3 (cons p d3))
  )
 )
 (setq s (list (vl-string-translate d1 d2 s)))
 (foreach p d3
  (setq s (apply (function append) (mapcar (function (lambda (a) (f1 a p))) s)))
 )
 (vl-remove "" s)
)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #41 on: July 27, 2010, 08:59:13 AM »
and the results

Quote
Elapsed milliseconds / relative speed for 16384 iteration(s):
    (LM:STRINGPARSERM3 ";;;;;;;;;;;;;Thi...)......1529 / 6.84 <fastest>
    (Q:STR:DELIM ";;;;;;;;;;;;;This;;;is...)......1919 / 5.45
    (MPARS ";;;;;;;;;;;;;This;;;is.a,;.T...)......2168 / 4.82
    (MULTIPARSE2 ";;;;;;;;;;;;;This;;;is...)......2387 / 4.38
    (MULTIPARSE1 ";;;;;;;;;;;;;This;;;is...)......2808 / 3.72
    (LM:STRINGPARSERM ";;;;;;;;;;;;;This...)......4633 / 2.26
    (LM:STRINGPARSERM2 ";;;;;;;;;;;;;Thi...).....10452 / 1.00 <slowest>

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #42 on: July 27, 2010, 09:07:08 AM »
and the results
...

Show the arguments of function calls to compare the speed ...

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #43 on: July 27, 2010, 09:13:32 AM »
and the results
...

Show the arguments of function calls to compare the speed ...

Right, yours is faster with longer strings

Code: [Select]
(setq _str (strcat "hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."))



(BenchMark
            '(
              (LM:StringParserM _str '("." "," ))
              (LM:StringParserM2 _str '("." "," ))
              (q:str:delim _str '("." "," ))
              (MultiParse2 _str '("." "," ))
              (MultiParse1 _str '("." "," ))
              (LM:StringParserM3 _str '("." ","))
              (mpars _str '("." ","))
             )
          )


Quote
Elapsed milliseconds / relative speed for 4096 iteration(s):
    (LM:STRINGPARSERM3 _STR (QUOTE ("." ...)......1841 / 6.89 <fastest>
    (MPARS _STR (QUOTE ("." ",")))................1872 / 6.78--------------------------------
    (Q:STR:DELIM _STR (QUOTE ("." ",")))..........2293 / 5.53
    (LM:STRINGPARSERM _STR (QUOTE ("." "...)......4883 / 2.60
    (MULTIPARSE2 _STR (QUOTE ("." ",")))..........8877 / 1.43
    (MULTIPARSE1 _STR (QUOTE ("." ","))).........10592 / 1.20
    (LM:STRINGPARSERM2 _STR (QUOTE ("." ...).....12683 / 1.00 <slowest>

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #44 on: July 27, 2010, 09:25:00 AM »
I get the opposite results ...  :-(
Code: [Select]
(setq _str (strcat "hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."
                  "the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you.welcome.to.the,swamp,hello.how.are.you."))
(BenchMark
            '(
              (q:str:delim _str '("." "," ))
              (LM:StringParserM3 _str '("." ","))
              (mpars _str '("." ","))
             )
          )
Code: [Select]
Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):

    (MPARS _STR (QUOTE ("." ",")))...............1155 / 1.28 <fastest>
    (LM:STRINGPARSERM3 _STR (QUOTE ("." ...).....1248 / 1.19
    (Q:STR:DELIM _STR (QUOTE ("." ","))).........1482 / 1 <slowest>

 
_$

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: -={ Challenge }=- Multi-Delimiter String Parsing
« Reply #45 on: July 27, 2010, 11:04:48 AM »
Code: [Select]
Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):

    (MSPLIT _STR (QUOTE ("." ",")))..............1500 / 1.66 <fastest>
    (MPARS _STR (QUOTE ("." ",")))...............2063 / 1.20
    (LM:STRINGPARSERM3 _STR (QUOTE ("." ...).....2484 / 1.00 <slowest>