Search list for items

Search list for items

etilley327KA
Advocate Advocate
485 Views
8 Replies
Message 1 of 9

Search list for items

etilley327KA
Advocate
Advocate

What is the easiest way to search a list to see if one of multiple objects exist in the list?

 

Ex: list: 22 41 39 18 32 19 25

Does 18 or 23 exist in the list?

0 Likes
Accepted solutions (1)
486 Views
8 Replies
Replies (8)
Message 2 of 9

Moshe-A
Mentor
Mentor

@etilley327KA  hi,

 

the best of best is Lee's (LM:ListDupes)

 

Moshe

 

;; List Duplicates  -  Lee Mac
;; Returns a list of items appearing more than once in a supplied list

(defun LM:ListDupes ( l )
    (if l
        (if (member (car l) (cdr l))
            (cons (car l) (LM:ListDupes (vl-remove (car l) (cdr l))))
            (LM:ListDupes (vl-remove (car l) (cdr l)))
        )
    )
)

(LM 

0 Likes
Message 3 of 9

john.kaulB9QW2
Advocate
Advocate
Accepted solution

MEMBER

 

(setq aList '(22 41 39 18 32 19 25))
(member 18 aList)
(member 23 aList)
another swamper
0 Likes
Message 4 of 9

paullimapa
Mentor
Mentor

can do it with AutoLISP or Visual Lisp

paulli_apa_0-1672340552226.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 9

Kent1Cooper
Consultant
Consultant

@etilley327KA wrote:

What is the easiest way to search a list to see if one of multiple objects exist in the list?

....


If all you need to know is whether or not either of them is in the list, and you don't need the list returned from the found item to the end as (member) does, and you don't need to know in what position in the list either item falls as (vl-position) reports, you can do this:

 

(or (member 18 aList) (member 23 aList))
 
That will return a simple T [True] if either one is [or both are] in the list, or nil if neither is.
 
Kent Cooper, AIA
0 Likes
Message 6 of 9

calderg1000
Mentor
Mentor

Regards @etilley327KA 

Try this code

 

(defun Fnx (lx x / n)
  (foreach i lx
    (if (= i x)
      (setq n T)
    )
  )
  (if (= n t)
    (princ "The Number exists...")
    (princ "The Number does not exist...")
  )
)

;_
(setq lt '(22 41 39 18 32 19 25))
(Fnx lt 25)
;"The Number exists..."

 


Carlos Calderon G
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 7 of 9

etilley327KA
Advocate
Advocate
Thanks! My list is a list of strings so I changed it to "18" and "23", but now its always coming back true. What am I doing wrong?
0 Likes
Message 8 of 9

paullimapa
Mentor
Mentor

Show us your lines of code show we can see where the error is


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 9 of 9

etilley327KA
Advocate
Advocate

Sorry, my bad. I see my error now. Thanks

0 Likes