Jump to content

[LISP] Parse/split list by element


ziele_o2k

Recommended Posts

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)

Link to comment
Share on other sites

(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"))

Link to comment
Share on other sites

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 by pBe
Link to comment
Share on other sites

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)))
   )
)

Link to comment
Share on other sites

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)))
   )
)

Link to comment
Share on other sites

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) '("))")))))
)

Link to comment
Share on other sites

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 ... :sweat:

Link to comment
Share on other sites

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) '("))")))))
)

 

 

:thumbsup:

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

Link to comment
Share on other sites

 

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 by ziele_o2k
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...