Comparing two lists

Comparing two lists

thomas.schive
Enthusiast Enthusiast
1,481 Views
10 Replies
Message 1 of 11

Comparing two lists

thomas.schive
Enthusiast
Enthusiast

Hello! 

I am trying to do something quite simple - it is just a problem of writing the correct LISP of doing so ...

I need 3 different lists:

 

List1 is a list that contains all layers in a drawing;

 

(defun LayersList ( / )
(vlax-for lay (vla-get-Layers
(vla-get-ActiveDocument (vlax-get-acad-object)))
(setq List1 (cons (vla-get-Name lay) List1))))

 

I think this code is okay as it creates list1 consisting of the layers in the drawing.

Another list (list2) is a comparison list I create and define myself - for now let's say that list2 is put through

 

(setq list2 '("1" "2" "3" "A" "B" "C"))

 

Finally, I would like to compare the list1 with the list2 and give me the values (layer names) that do comply and the ones that don't comply. The values that don't comply I would like to constitute the third list "list3". I have tried with "member", "mapcar" and a few others without succeeding so far...

Any ways of writing this code?

 

Thanks! 

0 Likes
Accepted solutions (2)
1,482 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Don't understand.

Give us imaginary examples of lst1, lst2 and desired outcome of lst3

 

lst3 is a result of comparison based on what? (hopefully, I could see that from given examples)

0 Likes
Message 3 of 11

thomas.schive
Enthusiast
Enthusiast

Okay, so let's say that I have in my DWG the current layers with layer names "0" "1" "2" "3" "A" "B" and "C". 

My first list "list1" will then be the layer names listed as above: ("0" "1" "2" "3" "A" "B" "C").

 

What I want to is to find the layer names in my drawing (list1) that does not exist in my reference list (list2). The reference list "list2" is defined, as an example: '("0" "1" "2" "A" "B"). Call it the layer names that should be in the drawing. The ones that differ, in this case "3" and "C" are the layers that are not named correctly according to list2. 

 

I want the ones that differ, "3" and "C" in this case, to be written into a list "list3" that is printed to the user to inform the user which layers are not named correctly according to the reference list "list2". 

0 Likes
Message 4 of 11

ВeekeeCZ
Consultant
Consultant

Like this?

 

(setq lst1 '("0" "1" "2" "3" "A" "B" "C"))
(setq lst2 '("0" "1" "2" "A" "B"))
(setq lst3 (vl-remove-if '(lambda (x) (vl-position x lst2)) lst1))

>> ("3" "C")

 

Message 5 of 11

hak_vz
Advisor
Advisor
Accepted solution
(setq list1 '("0" "1" "2" "3" "A" "B" "C") list2 '("0" "1" "2" "A" "B"))

(foreach el list1 
(if (not (member el list2)) 
       (setq list3 (cons el list3)))
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 6 of 11

thomas.schive
Enthusiast
Enthusiast

Could not get this one to work, but the answer below with the foreach seems to provide the desired results. 

Thanks to both! 

0 Likes
Message 7 of 11

ВeekeeCZ
Consultant
Consultant

That's unfortunate. 

You might need (vl-load-com) loaded. Then try

(vl-remove-if '(lambda (x) (vl-position x '("0" "1" "2" "A" "B"))) '("0" "1" "2" "3" "A" "B" "C"))

 

Foreach is good to know but mastering lists with such functions is essential! That's what LISP is all about.

Good luck!

0 Likes
Message 8 of 11

Kent1Cooper
Consultant
Consultant

Depending on what you intend to do with that information, it may be that the built-in >CAD Standards< features can handle it for you.

Kent Cooper, AIA
0 Likes
Message 9 of 11

thomas.schive
Enthusiast
Enthusiast

Hello again!

 

I was wondering - relating to this topic, whether the following code:

 

(defun Layerlist ( / )
(vlax-for lay (vla-get-Layers
(vla-get-ActiveDocument (vlax-get-acad-object)))
(setq List1 (cons (vla-get-Name lay) List1))
))

 

Could be changed into ONLY listing non-xref layers in List1?

Thanks for your help, very much appreciated! 

0 Likes
Message 10 of 11

ВeekeeCZ
Consultant
Consultant
Accepted solution

Just test the layer name if it includes *|*. Pretty much the same thing as you used HERE 

Message 11 of 11

hak_vz
Advisor
Advisor

See this post  and try to update your code.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.