Jump to content

Make separate list


plecs

Recommended Posts

Hello all.

I have a problem and I don't know how to solve it
I have a list

'(("0" "1" "red")  ("1" "1" "red") ("2" "1" "red")  ("4" "1" "red") ("0" "2" "blue")  ("1" "2" "blue") ("2" "4" "blue")  ("4" "5" "blue")  ("15" "2" "yellow")  ("16" "2" "yellow") ("21" "4" "yellow")  ("40" "5" "yellow"))

and I would like to separate them into several lists

ex:

 

(list1 '(("0" "1" "red")  ("1" "1" "red") ("2" "1" "red")  ("4" "1" "red")))

(list2 '(("0" "2" "blue")  ("1" "2" "blue") ("2" "4" "blue")  ("4" "5" "blue")))

(list3 '(("15" "2" "yellow")  ("16" "2" "yellow") ("21" "4" "yellow")  ("40" "5" "yellow")))

 

how would it be possible to do this with a lisp routine

thank you in advance

Link to comment
Share on other sites

If I understand correctly, this could be 1 way to approach it:

 


  (setq lst '(("0" "1" "red")  ("1" "1" "red") ("2" "1" "red")  ("4" "1" "red")
              ("0" "2" "blue")  ("1" "2" "blue") ("2" "4" "blue")  ("4" "5" "blue")
              ("15" "2" "yellow")  ("16" "2" "yellow") ("21" "4" "yellow")  ("40" "5" "yellow")))

  (setq red nil blue nil yellow nil)

  (foreach l lst
      (set (read (last l))
           (cons l (eval (read (last l))))))

 

One of the problems with (set) is figuring out how to localize the  return lists

 

Good luck  -David

 

 

Link to comment
Share on other sites

david beat me to it...


(defun tst ( / m lst l1 l2 l3 l4) (vl-load-com)
  (defun m (l v) (vl-remove-if-not '(lambda (x)(member v x)) l))
  (setq lst '(("0" "1" "red") ("1" "1" "red") ("2" "1" "red") ("4" "1" "red")
              ("0" "2" "blue") ("1" "2" "blue") ("2" "4" "blue") ("4" "5" "blue")
              ("15" "2" "yellow") ("16" "2" "yellow") ("21" "4" "yellow") ("40" "5" "yellow")))
  (setq l1 (m lst "red") l2 (m lst "blue") l3 (m lst "yellow"))
  (princ (strcat "\nList Red = " (vl-princ-to-string l1)))
  (princ (strcat "\nList Blue = " (vl-princ-to-string l2)))
  (princ (strcat "\nList Yellow = " (vl-princ-to-string l3)))
  (princ)
)

 

Link to comment
Share on other sites

Like david as you have a sorted list can use the set to make lst1 lst2 etc looking at is the next value color the same if yes then add it to list, this way can have as many colors as you want, maybe make varaible lstred. 

 

I need some time to think about it.

Link to comment
Share on other sites

If you worry about localizing then don't and create assoc list like :

(  ("red"    . (( "0" "1")( "1" "1")( "2" "1")( "4" "1")))
   ("blue"   . (( "0" "2")( "1" "2")( "2" "4")( "4" "5")))
   ("yellow" . (("15" "2")("16" "2")("21" "4")("40" "5")))
)


;;; like this :

(setq lst '(("0" "1" "red")("1" "1" "red")("2" "1" "red")("4" "1" "red")
            ("0" "2" "blue")("1" "2" "blue")("2" "4" "blue")("4" "5" "blue")
            ("15" "2" "yellow")("16" "2" "yellow")("21" "4" "yellow")("40" "5" "yellow")))

(defun t1 ( l / c r) (foreach x l (if (assoc (setq c (last x)) r)
  (setq r (subst (cons c (cons x (cdr (assoc c r))))(assoc c r) r))(setq r (cons (cons c (list x)) r))))(reverse r))

(defun t2 ( l / c r) (foreach x l (if (assoc (setq c (last x)) r)
  (setq r (subst (cons c (cons (reverse (cdr (reverse x))) (cdr (assoc c r)))) (assoc c r) r))
    (setq r (cons (cons c (list (reverse (cdr (reverse x))))) r)))) (reverse r))

(setq l1 (t1 lst))
(setq l2 (t2 lst))
Edited by rlx
Link to comment
Share on other sites

On 4/23/2021 at 6:03 PM, BIGAL said:

I need some time to think about it.

 

I've played with recording the atoms-family as the first call in a routine.  Then compare that to a closing atoms-family for new atoms.

maybe a (boundp) test

 

Nothing ever jumped out as a foolproof method.  So I use (set) sparingly in this scenario for public snippets

 

A single session in-house call is useful some times

 

I looked and was surprised to have 470 .lsp files that includes    (set (read   I would not have guessed I use it so much !!!

 

Regards  -David

 

 

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