Jump to content

List Contents Compaison


Happy Hobbit

Recommended Posts

I'm trying to find a way of comparing the contents of a list to see if all the items are of the same value.

e.g: '(2 3 2 2 2)

This is obviously numeric, however it would be good if it worked on a list of text too

Is there an efficient way of doing it?

The only way I can think of doing it is with the following:

(defun c:test ( / a b c i)
(setq a '(2 2 3))
(setq b (car a))
(setq c 0) 
(setq i 1)
(repeat (-(length a)1)
	(if (/= b (nth i a))
	  (setq c 1)
	  )
  (setq i (1+ i))
)
(if (eq c 0)
 (princ "\nEqual")
 (princ "\nNot Equal")
)  
(princ)
)

 

But I bet there's a better way

Edited by Happy Hobbit
Link to comment
Share on other sites

search for vl-every :-)

 

http://www.cadtutor.net/forum/showthread.php?85929-vl-every

 

http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-8BF61C9E-1382-4168-A778-FEA394A361CC

 

(setq lst1 (list 1 1 1) lst2 (list 1 2 3))

 

(vl-every '= lst1 (cdr lst1))

(vl-every '= lst2 (cdr lst2))

 

gr. Rlx

Edited by rlx
Link to comment
Share on other sites

Thanks for that Rix, I'm not too hot on VL functions & never heard of vl-every

 

There were one or two errors in my code above, since amended

Link to comment
Share on other sites

Thanks for that Rix, I'm not too hot on VL functions & never heard of vl-every

 

There were one or two errors in my code above, since amended

 

till a couple of years ago neither did I but it really pays of to invest (vl-)some time in these functions

 

gr. Rlx

Link to comment
Share on other sites

Excellent Tharwat, thank you. One simple line of code replaces about 9 lines in my code. It does make me wonder how many AutoLISP functions are an amalgam of other functions though

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