Help With List Comparison

Help With List Comparison

mgorecki
Collaborator Collaborator
798 Views
8 Replies
Message 1 of 9

Help With List Comparison

mgorecki
Collaborator
Collaborator

Hello,

I have a couple of lists that I want to compare items and see if there is a match.

padLocAndNumList = (((1283.66 6030.51) "BUMP_4") ((1481.42 6030.51) "BUMP_5")  ((1382.54 6129.39) "BUMP_1") ((1580.3 6129.39) "BUMP_2") ((1778.06 6129.39) "BUMP_3"))

padLocAndNumList is a list of a block centerpoint and its block attribute.  The function uses this to create each:

(setq padLocNum (list (list (car centerPt) (cadr centerPt)) padNumber))

Each padLocNum gets added to the padLocAndNumList.

 

polyEndpoints = ((1382.54 6129.39) (1580.3 6129.39))

polyEndPoints are the endpoints of a polyline that were created using a function to get the first and last points of a polyline.  The function uses this to create the list:

(setq coordList (append coordList (list (list (car vertex)(cadr vertex)))))

This gets returned to polyEndpoints.

 

Now I want to take the polyEndPoints and search the padLocAndNumList to find a coordinate match (either start or end point).

Admittedly I found this code and I understand what it does through a lot of testing, but I still don't understand why it doesn't work with my lists.

 

(Findmatches polyEndpoints padLocAndNumList)

(defun Findmatches (plineEnt padList / match matches)
 (foreach pad padList
  (if (setq match (find plineEnt pad))
   (setq matches (cons match matches))
  )
 )
 (setq matches (reverse matches))
)

(defun find (plineEnt pad / match)
 (foreach item2 plineEnt (if (vl-position item2 pad) (setq match pad)))
 match
)

 

 

When I watch in the watch window it looks like it matches the start point with the start point from Bump_1 and the end point with the end point from Bump_2, but it doesn't seem to work.  I assume its a problem with my lists, but I can't figure this one out.

I would appreciate any help on this.

Thanks

 

 

 

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

ronjonp
Mentor
Mentor
Accepted solution

@mgorecki 

Are you looking for something like this?

 

(setq padlocandnumlist
       '(((1283.66 6030.51) "BUMP_4")
	 ((1481.42 6030.51) "BUMP_5")
	 ((1382.54 6129.39) "BUMP_1")
	 ((1580.3 6129.39) "BUMP_2")
	 ((1778.06 6129.39) "BUMP_3")
	)
)
(foreach pt coordlist
  (if (setq r (vl-some '(lambda	(x)
			  (if (equal pt (car x) 1e-8)
			    x
			  )
			)
		       padlocandnumlist
	      )
      )
    ;; Return a list of matches
    (setq rtn (cons r rtn)
	  ;; Remove it from the list so no duplicates
	  padlocandnumlist
	   (vl-remove r padlocandnumlist)
    )
  )
)

 

 

0 Likes
Message 3 of 9

mgorecki
Collaborator
Collaborator

Hello Ron,

Thank you very much, that works great.  I don't really understand "lambda", so you gave me something to study.

0 Likes
Message 4 of 9

ronjonp
Mentor
Mentor

@mgorecki wrote:

Hello Ron,

Thank you very much, that works great.  I don't really understand "lambda", so you gave me something to study.


Glad to help 🙂 If you have large data sets it might speed this up if you presort the lists by X coord. VL-SOME stops when it finds a match so if they are in similar order the result will be found faster.

(setq padlocandnumlist (vl-sort padlocandnumlist '(lambda (r j) (< (caar r) (caar j)))))
(setq coordlist (vl-sort coordlist '(lambda (r j) (< (car r) (car j)))))
0 Likes
Message 5 of 9

mgorecki
Collaborator
Collaborator

Those work great, I'll definately have to use those!

0 Likes
Message 6 of 9

mgorecki
Collaborator
Collaborator

Hi Ron, I hate to bug you again, but I'm hoping you can help me with a similar issue:

polyEndpoints = ((1778.06 6129.39) (1874.79 6268.21))

(setq polyConnectionList

 '(((1481.42 6030.51) (1283.66 6030.51) (<Entity name: 17d4469d8b0>))

  ((1876.94 6030.51) (1679.18 6030.51) (<Entity name: 17d4469d890>))

  ((1580.3 6129.39) (1778.06 6129.39) (<Entity name: 17d4469d880>))

  ((1382.54 6129.39) (1580.3 6129.39) (<Entity name: 17d4469d870>))))

 

As a test, I'm using a variant of what you had showed me earlier to search the list for the coordinate in the endpoint position:

(foreach pt polyEndpoints
(if (setq plineInfo (vl-some '(lambda (x) (if (equal pt (cadr x) 1e-8) x)) polyConnectionList))
(setq newList (cons plineInfo newList)))
)

In my mind (which is fried at this point), it should match the second set in the 3rd list, but it doesn't.  Do you know why it doesn't?

What I'm really trying to do is to find any connection to either the first or second point of polyEndpoints from polyConnectionList.  

0 Likes
Message 7 of 9

mgorecki
Collaborator
Collaborator

Never mind, I found the problem.  I just removed the (cadr for x)

(if (setq plineInfo (vl-some '(lambda (x) (if (equal pt x 1e-8) x)) polyConnectionList))

0 Likes
Message 8 of 9

ronjonp
Mentor
Mentor

Works for me:

ronjonp_0-1692134346737.png

 

0 Likes
Message 9 of 9

mgorecki
Collaborator
Collaborator

Hi, Yep I posted my reply too quickly.  My newlist did catch it, but I was looking at plineInfo which gets cleared out.  I updated the command to look for either end of the pline to find a match:

(if (setq plineInfo (vl-some '(lambda (x) (if (or (equal pt (car x) 1e-8)(equal pt (cadr x) 1e-8)) x)) polyConnectionList))
(setq newList (cons plineInfo newList)))

 

Thanks again for checking.

0 Likes