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

0 Members and 1 Guest are viewing this topic.

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: 8706
  • 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: 1631
  • 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: 8706
  • 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: 1631
  • 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: 12914
  • 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: 12914
  • 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: 8706
  • 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: 8706
  • 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>

 
_$