ziele_o2k Posted July 27, 2018 Posted July 27, 2018 Any other propositions to parse/split list by element: (defun pz:LST_Parse (@lst @Sep @Rbl / _el _res) (setq _el nil) (foreach # @lst (if (= @Sep #) (setq _res (cons _el _res) _el nil) (setq _el (append _el (list #))) ) ) (setq _res (cons _el _res)) (reverse (if @Rbl (vl-remove nil _res) _res) ) ) Example: (cd:LST_Parse '("#" "ASD" "ASDD" "DSA" "#" "FDS" "gfsfg") "#" nil) (cd:LST_Parse '("#" "ASD" "ASDD" "DSA" "#" "#" "FDS" "gfsfg") "#" t) Quote
satishrajdev Posted July 27, 2018 Posted July 27, 2018 (defun foo (l c m / o n) (while l (if (/= (car l) c) (setq n (cons (car l) n)) (setq o (cons (reverse n) o) n nil ) ) (setq l (cdr l)) ) (vl-remove m (reverse (cons n o))) ) Example: _$ (foo '("#" "ASD" "ASDD" "DSA" "#" "#" "FDS" "gfsfg" "#") "#" t) (nil ("ASD" "ASDD" "DSA") nil ("FDS" "gfsfg") nil) _$ (foo '("#" "ASD" "ASDD" "DSA" "#" "#" "FDS" "gfsfg" "#") "#" nil) (("ASD" "ASDD" "DSA") ("FDS" "gfsfg")) Quote
pBe Posted July 27, 2018 Posted July 27, 2018 (edited) Any other propositions to parse/split list by element: (defun [b][color="blue"]pz[/color][/b]:LST_Parse (... ) Example: ([color="blue"]cd[/color]:LST_Parse '("#" "ASD" "ASDD" "DSA" "#" "FDS" "gfsfg") "#" nil) Makes you wonder... Edited July 27, 2018 by pBe Quote
Lee Mac Posted July 27, 2018 Posted July 27, 2018 http://www.theswamp.org/index.php?topic=53354.0 Quote
Lee Mac Posted July 27, 2018 Posted July 27, 2018 Another two, depending on the desired behaviour: (defun parselst ( sep lst / tmp ) (if (setq lst (vl-member-if '(lambda ( x ) (setq tmp (cons x tmp)) (= sep x)) lst)) (cons (reverse (cdr tmp)) (parselst sep (cdr lst))) (list (reverse tmp)) ) ) (defun parselst2 ( sep lst / tmp ) (cond ( (null lst) lst) ( (null (setq lst (vl-member-if '(lambda ( x ) (setq tmp (cons x tmp)) (= sep x)) lst))) (list (reverse tmp)) ) ( (cdr tmp) (cons (reverse (cdr tmp)) (parselst2 sep (cdr lst))) ) ( (parselst2 sep (cdr lst))) ) ) Quote
Lee Mac Posted July 27, 2018 Posted July 27, 2018 Another variation: (defun parselst3 ( sep lst / idx tmp ) (cond ( (null lst) lst) ( (null (setq idx (vl-position sep lst))) (list lst) ) ( (< 0 idx) (repeat idx (setq tmp (cons (car lst) tmp) lst (cdr lst))) (cons (reverse tmp) (parselst3 sep (cdr lst))) ) ( (parselst3 sep (cdr lst))) ) ) Quote
Lee Mac Posted July 27, 2018 Posted July 27, 2018 Another, for lists with string items only: (defun parselst4 ( sep lst ) (if lst (read (apply 'strcat (append '("((") (mapcar '(lambda ( x ) (if (= x sep) ")(" (vl-prin1-to-string x))) lst) '("))"))))) ) Quote
rlx Posted July 27, 2018 Posted July 27, 2018 Another, for lists with string items only: (defun parselst4 ( sep lst ) (if lst (read (apply 'strcat (append '("((") (mapcar '(lambda ( x ) (if (= x sep) ")(" (vl-prin1-to-string x))) lst) '("))"))))) ) thanx Lee , my body was already melting cause of the heatwave and now my mind is melting too.... again , thank you so much ... Quote
hanhphuc Posted July 27, 2018 Posted July 27, 2018 my $0.02 if content of string has "#" ? keep or parse ? '("#" "ASD" "AS[color="red"]#[/color]DD" "DSA" "1 ""2" "#" "FDS" "gfsfg" "#") ;also string has many possibilities [color="purple"]"#" " #" "# " " # " "# #"[/color] Another, for lists with string items only: (defun parselst4 ( sep lst ) (if lst (read (apply 'strcat (append '("((") (mapcar '(lambda ( x ) (if (= x sep) ")(" (vl-prin1-to-string x))) lst) '("))"))))) ) nice idea though (parselst4 "#" '("#" "ASD" "ASDD" "DSA" " #" "#" " #" "FDS" "gfsfg" "#" " # ") ) (nil ("ASD" "ASDD" "DSA") nil ([color="red"]" #"[/color] "FDS" "gfsfg") ([color="red"]" # "[/color])) (parselst4 "#" '("#" "ASD" "ASDD" "DSA" " #" "#" " #" "FDS" "gfsfg" "#" " # ") ) (nil ("ASD" "ASDD" "DSA" [color="red"]" #"[/color]) ([color="red"]" #"[/color] "FDS" "gfsfg") ([color="red"]" # "[/color])) same bug here - brackets must be paired otherwise ; error: malformed list or extra right paren on input Quote
rlx Posted July 28, 2018 Posted July 28, 2018 Well , that's (partly) a matter of dic : data integrity check Quote
ziele_o2k Posted July 30, 2018 Author Posted July 30, 2018 (edited) http://www.theswamp.org/index.php?topic=53354.0 Thanks Lee (defun pz:LST_Parse (@Lst @Prd @Rbl / _el _res) (setq _el nil) (foreach # @Lst (if (apply @Prd (list #)) (setq _res (cons _el _res) _el nil) (setq _el (append _el (list #))) ) ) (setq _res (cons _el _res)) (reverse (if @Rbl (vl-remove nil _res) _res) ) ) Edited July 30, 2018 by ziele_o2k Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.