(defun _Whargarbl ( text / i j temp result )
(and
(setq i (vl-string-position (ascii ",") text nil t))
(setq j (vl-string-position (ascii ".") (setq temp (substr text 1 i)) nil t))
(setq result (substr temp (+ 2 j)))
)
result
)
AWESOME! Thanks!
(defun _StringSplit ( keys string )
;; (_StringSplit ":,." "Quote: This too, shall pass.")
;; >> ("Quote" " This too" " shall pass")
(mapcar 'vl-list->string
( (lambda ( codes delims / token result )
(foreach x codes
(if (member x delims)
(setq result (cons token result) token nil)
(setq token (cons x token))
)
)
(cons token result)
)
(reverse (vl-string->list string))
(vl-string->list keys)
)
)
)
(defun _StringSplit ( delims text )
;; (_StringSplit ":;," "A:B,C;D")
;; >> ("A" "B" "C" "D")
( (lambda ( foo delims / result p )
(while (setq p (car (vl-sort (mapcar 'foo delims) '>)))
(setq
result (cons (substr text (+ 2 p)) result)
text (substr text 1 p)
)
)
(cons text result)
)
(lambda (d) (vl-string-position d text nil t))
(vl-string->list delims)
)
)
(defun _StringSplit ( keys string )
;; (_StringSplit ":;," "A:B,C;D")
;; >> ("A" "B" "C" "D")
( (lambda ( delims / i result )
(repeat (setq i (strlen string))
(if (member (vl-string-elt string (setq i (1- i))) delims)
(setq
result (cons (substr string (+ 2 i)) result)
string (substr string 1 i)
)
)
)
(cons string result)
)
(vl-string->list keys)
)
)
(defun ALE_String_ToListByKeys (InpStr KysStr / NewKys CarDlm SttPos EndPos TmpLst)
(setq
CarDlm (substr KysStr 1 1) NewKys CarDlm
NewKys (repeat (strlen KysStr) (setq NewKys (strcat CarDlm NewKys)))
InpStr (vl-string-translate KysStr NewKys InpStr)
CarDlm (ascii CarDlm) SttPos 0
EndPos (vl-string-position CarDlm InpStr)
)
(while EndPos
(setq
TmpLst (cons (substr InpStr (1+ SttPos) (- EndPos SttPos)) TmpLst)
SttPos (1+ EndPos) EndPos (vl-string-position CarDlm InpStr SttPos)
)
)
(reverse (cons (substr InpStr (1+ SttPos)) TmpLst))
)
Code - Auto/Visual Lisp: [Select]
This reminds me of the form of recursion used when implementing the quicksort algorithm - very good Evgeniy :-)
(defun AM1 (d s / n c p e l)
(setq
c (substr d 1 1) n c
n (repeat (strlen d) (setq n (strcat c n)))
s (vl-string-translate d n s)
c (ascii c) p 0
e (vl-string-position c s)
)
(while e
(setq
l (cons (substr s (1+ p) (- e p)) l)
p (1+ e) e (vl-string-position c s p)
)
)
(reverse (cons (substr s (1+ p)) l))
)
(setq Keys ":;," String "A:B,C;D")
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 131072 iteration(s):
(AM1 KEYS STRING)................1703 / 1.83 <fastest>
(AM1 KEYS STRING)................1704 / 1.82
(_StringSplit3 KEYS STRING)......1766 / 1.76
(_StringSplit3 KEYS STRING)......1781 / 1.75
(_StringSplit1 KEYS STRING)......1875 / 1.66
(_StringSplit1 KEYS STRING)......1906 / 1.63
(F KEYS STRING)..................2359 / 1.32
(F KEYS STRING)..................2360 / 1.32
(_StringSplit2 KEYS STRING)......3015 / 1.03
(F1 KEYS STRING).................3046 / 1.02
(_StringSplit2 KEYS STRING)......3094 / 1
(F1 KEYS STRING).................3109 / 1 <slowest>
(setq Keys ":;," String "A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:A:B,C;D:")
Elapsed milliseconds / relative speed for 16384 iteration(s):
(AM1 KEYS STRING)................1438 / 4.42 <fastest>
(AM1 KEYS STRING)................1453 / 4.38
(_StringSplit1 KEYS STRING)......2235 / 2.85
(_StringSplit1 KEYS STRING)......2235 / 2.85
(_StringSplit3 KEYS STRING)......3266 / 1.95
(_StringSplit3 KEYS STRING)......3312 / 1.92
(_StringSplit2 KEYS STRING)......5671 / 1.12
(_StringSplit2 KEYS STRING)......5750 / 1.11
(F KEYS STRING)..................5797 / 1.1
(F KEYS STRING)..................5813 / 1.09
(F1 KEYS STRING).................6297 / 1.01
(F1 KEYS STRING).................6359 / 1 <slowest>
(setq Keys ":;," String "A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
(repeat 4 (setq String (strcat String ";" String))
Elapsed milliseconds / relative speed for 16384 iteration(s):
(AM1 KEYS STRING).................1532 / 12.07 <fastest>
(AM1 KEYS STRING).................1609 / 11.49
(F KEYS STRING)...................3437 / 5.38
(F KEYS STRING)...................3453 / 5.35
(_StringSplit2 KEYS STRING).......4516 / 4.09
(_StringSplit2 KEYS STRING).......4609 / 4.01
(_StringSplit3 KEYS STRING)......15906 / 1.16
(_StringSplit3 KEYS STRING)......15906 / 1.16
(F1 KEYS STRING).................16375 / 1.13
(F1 KEYS STRING).................16454 / 1.12
(_StringSplit1 KEYS STRING)......17609 / 1.05
(_StringSplit1 KEYS STRING)......18485 / 1 <slowest>
Test order:(Benchmark '(
(_StringSplit1 Keys String)
(_StringSplit2 Keys String)
(_StringSplit3 Keys String)
(AM1 Keys String)
(f Keys String)
(f1 Keys String)
(_StringSplit1 Keys String)
(_StringSplit2 Keys String)
(_StringSplit3 Keys String)
(AM1 Keys String)
(f Keys String)
(f1 Keys String)
) )
Recursion is beautiful (very) but often is slower...
(f3 "." "A.B\"C.D")
Recursion is beautiful (very) but often is slower...speed of some functions depends on the length of both arguments
>>> (read (strcat "(\"" s "\")"))Recursion is beautiful (very) but often is slower...
Hello Marc!
Yes!
Recursion is very slow.
The new version only for speed...
:-)Code - Auto/Visual Lisp: [Select]
New test:Recursion is beautiful (very) but often is slower...speed of some functions depends on the length of both arguments
try increasing the length of delimiters also
(setq Keys ":;,123456789abcd" String "A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
Elapsed milliseconds / relative speed for 32768 iteration(s):
(AM1 KEYS STRING)................1781 / 9.61 <fastest>
(AM1 KEYS STRING)................2329 / 7.35
(_STRINGSPLIT1 KEYS STRING)......4547 / 3.76
(_STRINGSPLIT1 KEYS STRING)......4890 / 3.5
(_STRINGSPLIT3 KEYS STRING)......4922 / 3.48
(_STRINGSPLIT3 KEYS STRING)......5468 / 3.13
(F1 KEYS STRING).................6469 / 2.64
(F1 KEYS STRING).................6750 / 2.53
(F KEYS STRING)..................9844 / 1.74
(F KEYS STRING).................10546 / 1.62
(_STRINGSPLIT2 KEYS STRING).....16625 / 1.03
(_STRINGSPLIT2 KEYS STRING).....17110 / 1 <slowest>
(setq Keys ":;,123456789abcd" String "A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
(repeat 4 (setq String (strcat String ";" String)))
Elapsed milliseconds / relative speed for 2048 iteration(s):
(AM1 KEYS STRING)................1203 / 21.74 <fastest>
(AM1 KEYS STRING)................1468 / 17.82
(_STRINGSPLIT1 KEYS STRING)......4093 / 6.39
(_STRINGSPLIT1 KEYS STRING)......4562 / 5.73
(_STRINGSPLIT3 KEYS STRING)......5093 / 5.14
(_STRINGSPLIT3 KEYS STRING)......5735 / 4.56
(F KEYS STRING)..................9985 / 2.62
(F KEYS STRING).................12672 / 2.06
(_STRINGSPLIT2 KEYS STRING).....19579 / 1.34
(_STRINGSPLIT2 KEYS STRING).....21281 / 1.23
(F1 KEYS STRING)................24094 / 1.09
(F1 KEYS STRING)................26157 / 1 <slowest>
new version, оnly for the sake of comparison, the speed...I get error in second sample:
<>
I do not like such a long code. :?
Comando: (setq Keys ":;," String "A:B,C;D")
"A:B,C;D"
Comando: (f5 Keys String)
("A" "B" "C" "D")
Comando: (setq Keys ":;,123456789abcd" String "A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
"A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu"
Comando: (f5 Keys String)
; errore: tipo di argomento errato: numberp: nil
Comando: (f1 Keys String)
("A" "" "" "" "" "" "" "" "" "0" "s" "fghjklò" "s" "fghjklò" "By" "qwuy" "quyqw" "iu" "" "" "" "" "" "" "" "" "Cs" "kjs" "kjs" "jk" "" "" "" "" "Duyyuwe" "" "" "" "" "" "ijiu")
I get error in second sample:Code: [Select]Comando: (setq Keys ":;,123456789abcd" String "A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
"A1234567890asdfghjklòasdfghjklò:Bybqwuybquyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu"
Comando: (f5 Keys String)
; errore: tipo di argomento errato: numberp: nil
_$ (f5 ";," "A;B,C;D,E")
("A" "B" "C;D,E")
_$ (f6 ";," "A;B,C;D,E")
("" ";" ",C;D,E")
_$ (f7 ";," ",A;B,C;D,E;")
(",A" "B" "C" "D" "E" "")
(setq Keys ":;,123456789abcd" String "A1234567890absdfghjklòasdfghjklò:Bybqwuybqcuyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
(repeat 4 (setq String (strcat String ";" String)))
Elapsed milliseconds / relative speed for 32768 iteration(s):
(F5 KEYS STRING)..................1219 / 325.2 <fastest>
(F5 KEYS STRING)..................1235 / 320.99
(AM1 KEYS STRING)................20016 / 19.81
(AM1 KEYS STRING)................20047 / 19.77
(_STRINGSPLIT1 KEYS STRING)......67750 / 5.85
(_STRINGSPLIT1 KEYS STRING)......68172 / 5.82
(_STRINGSPLIT3 KEYS STRING)......84281 / 4.7
(_STRINGSPLIT3 KEYS STRING)......84344 / 4.7 <slowest>
Elapsed milliseconds / relative speed for 2048 iteration(s):
(AM1 KEYS STRING)...............1281 / 4.29 <fastest>
(AM1 KEYS STRING)...............1313 / 4.19
(F7 KEYS STRING)................2313 / 2.38
(F7 KEYS STRING)................2390 / 2.3
(_STRINGSPLIT1 KEYS STRING).....4188 / 1.31
(_STRINGSPLIT1 KEYS STRING).....4375 / 1.26
(_STRINGSPLIT3 KEYS STRING).....5390 / 1.02
(_STRINGSPLIT3 KEYS STRING).....5500 / 1 <slowest>
(defun f-MR ( d s / d1 dl k ss c pl z l )
(while (and (setq d1 (substr d 1 1)) (/= d1 ""))
(setq d (substr d 2))
(setq dl (cons d1 dl))
)
(foreach d1 dl
(setq k -1 ss s)
(while (and (setq c (substr ss 1 1)) (/= c ""))
(setq ss (substr ss 2))
(setq k (1+ k))
(if (= c d1)
(setq pl (cons k pl))
)
)
)
(setq pl (vl-sort pl '<))
(foreach p pl
(if (null z)
(setq z 1)
)
(setq l (cons (substr s z (1+ (- p z))) l))
(setq z (+ p 2))
)
(setq l (cons (substr s z) l))
(vl-remove "" (reverse l))
)
Here is one more, huge one and not so elegant as MP's or Evgeniy's, but test it... To me it looks fine...
<>
(setq Keys ":;,123456789abcd" String "A1234567890absdfghjklòasdfghjklò:Bybqwuybqcuyqwbiu89897879,Csdkjsdkjsdjk3443;Duyyuwe878723ijiu")
(repeat 4 (setq String (strcat String ";" String)))
(AM1 KEYS STRING).......1328 / 53.78 <fastest>
(F-MR KEYS STRING).....71422 / 1 <slowest>
Comando: (F-MR KEYS STRING)
("A" "0" "s" "fghjklò" "s" "fghjklò" "By" "qwuy" "q" "uyqw" "iu" "Cs" "kjs" "kjs" "jk" "Duyyuwe" "ijiu" "A" "0" "s" "fghjklò" ...)
Comando: (AM1 KEYS STRING)
("A" "" "" "" "" "" "" "" "" "0" "" "s" "fghjklò" "s" "fghjklò" "By" "qwuy" "q" "uyqw" "iu" "" "" "" "" "" "" "" "" "Cs" "kjs" ...)
<>I would suggest to change (member (car strlst) keylst) with (vl-position (car strlst) keylst)
The VLE functions are built into Bricscad or can be loaded into ACAD. (See attachment, also see link for supported vle-functions)
https://www.bricsys.com/bricscad/help/en_US/V16/DevRef/
(https://www.bricsys.com/bricscad/help/en_US/V16/DevRef/)
(defun vle-string-split ( keys string / retlst idx len start keylst strlst )
(setq idx 1 start 1 len (strlen string))
(setq keylst (vl-string->list keys))
(setq strlst (cons 0 (vl-string->list string)))
(while (setq strlst (cdr strlst))
(if (vl-position (car strlst) keylst) ; <<<
(setq retlst (cons (substr string start (- idx start)) retlst)
idx (1+ idx)
start idx
)
(setq idx (1+ idx))
)
)
(if retlst
(reverse (cons (substr string start (1+ (- len start))) retlst))
(list string)
)
)