How to Search a List Question?

How to Search a List Question?

James.Pascual
Enthusiast Enthusiast
582 Views
5 Replies
Message 1 of 6

How to Search a List Question?

James.Pascual
Enthusiast
Enthusiast

I work in manufacturing, and I'm trying to automate Bill of Materials.  I have a list with sublist that represents parts.

 

(setq PartList (("HM213ST" 22.0 1) ("GF200" 22.0 1) ("E150" 26.0 1) ("E260" 26.0 1)))

 

Each sublist is formated like so: ([Part number] [length] [Quatity]), so for example of the first part in my list would be:

Part# = HM213ST

Length = 22.0"

Qty = 1

 

The issue is if my lisp routine wants to add a new part to this list I want to avoid duplicate parts that have the same part number and length.

 

So how would I accomplish if I want to add a new part ("HM213ST" 22.0 5) to my existing list by:

First look through the sublist of parts and check if "HM213ST" with a length of 22.0,

if it does exist only add my quantity 5 to the caddr qty 1.

so end resault list would look like (("HM213ST" 22.0 6) ("GF200" 22.0 1) ("E150" 26.0 1) ("E260" 26.0 1))

lastly, if it didn't exist append the new part to the end of the PartList list.

 

Thanks in advance for any help!

 

0 Likes
Accepted solutions (1)
583 Views
5 Replies
Replies (5)
Message 2 of 6

dgorsman
Consultant
Consultant

Have a look at the functions (assoc ...) and (subst ...), and possibly (member ...) and (vl-remove ...).  You'll also want to consider adding new items at the start of the list rather than the end - LISP works lists from front-to-back so its a little easier that way.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 3 of 6

ВeekeeCZ
Consultant
Consultant

How about have a list like this:

 

((("E260" . 26.0) . 1) (("E150" . 26.0) . 1) (("GF200" . 22.0) . 1) (("HM213ST" . 22.0) . 8))

 

which you can built with this

(cons (cons (cons part len) qua) PartList)

 

The advantage is that you can easily access a member using assoc.

 

Here is your sample.

 

Spoiler
(defun c:test (/ PartList)

  (foreach e '(("HM213ST" 22.0 6)
	       ("GF200" 22.0 1)
	       ("HM213ST" 22.0 2)
	       ("E150" 26.0 1)
	       ("E260" 26.0 1)
	      )
    (setq p  (nth 0 e)
	  l  (nth 1 e)
	  q  (nth 2 e)
	  pl (cons p l)
    )

    (if	(setq a (assoc pl PartList))
      (setq PartList (subst (cons pl (+ (cdr a) q))
			    a
			    PartList
		     )
      )
      (setq PartList (cons (cons pl q) PartList))
    )
  )
  (prin1 PartList)
  (princ)
)
0 Likes
Message 4 of 6

martti.halminen
Collaborator
Collaborator

Your specification could use a little clarification: is it possible to have instances of the same part with different length?

So you'd have for instance ... ("E150" 26.0 1) ("E150" 20.0 3)...

-- 

 

0 Likes
Message 5 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....  I have a list with sublist that represents parts.

(setq PartList (("HM213ST" 22.0 1) ("GF200" 22.0 1) ("E150" 26.0 1) ("E260" 26.0 1)))

.... I want to avoid duplicate parts that have the same part number and length.

.... I want to add a new part ("HM213ST" 22.0 5) to my existing list by:

.....

so end resault list would look like (("HM213ST" 22.0 6) ("GF200" 22.0 1) ("E150" 26.0 1) ("E260" 26.0 1))

.... 


Yes, that can be done.  Lightly tested:

 

(setq
PartList '(("HM213ST" 22.0 1) ("GF200" 22.0 1) ("E150" 26.0 1) ("E260" 26.0 1))
NewEntry '("HM213ST" 22.0 5)
); setq

(defun C:AddEntry (/ TempList done OldEntry) (setq TempList PartList) ; working list separate from original (while (and (not done); haven't found this part number at this length yet (setq OldEntry (assoc (car NewEntry) TempList)); part number still in working list ); and (if (= (cadr OldEntry) (cadr NewEntry)); same length? (setq ; then NewEntry (list (car OldEntry) (cadr OldEntry) (+ (caddr OldEntry) (caddr NewEntry))) ; add new quantity number to old quantity number PartList (subst NewEntry OldEntry Partlist); & update in original List done T ; stop (while) loop ); setq (setq TempList (cdr (member OldEntry Templist))); else ; take up through that length of that part off working list, look again at remainder ); if ); while (if (not OldEntry); never found this part at that length? (setq PartList (cons NewEntry Partlist)); add this part at this length to overall list ); if ); defun
Kent Cooper, AIA
Message 6 of 6

James.Pascual
Enthusiast
Enthusiast

marttihalminen, yes sorry if my description wasn't clear enough, yes one part number can have multipe lengths.  I can only combine the parts that have the same Part Number and Length.  Also to BeekeeCZ, I can't change the formating of the list because of lots of existing routines that are running that list format.

 

Kent Cooper though solves exactly what I needed thank you!  I tested it and it does every step and condition I need.  I'm using your routine to learn what i need so thanks again!

0 Likes