Checking that all elements of a list match a condition

Checking that all elements of a list match a condition

spencer.robertsU8DLN
Enthusiast Enthusiast
622 Views
2 Replies
Message 1 of 3

Checking that all elements of a list match a condition

spencer.robertsU8DLN
Enthusiast
Enthusiast

As the title says, I'm trying to validate whether all given values in a sublist meet a condition or not, then assign them to a list for later processing. In the below example, I would expect to end up with nothing in eg1, 4.0 and 2.0 sublists in eg2 and all others in eg3, but after running, only eg3 will have any contents and it will only be the 3.0 sublist. 

 

(setq r1 (cons 0 101.35))
	(setq r2 (cons 152.025 253.375))
	(setq edgegroup '(("OFFSET" nil) (3.0 (1.375 7.375 35.875 40.875 91.875 102.875 107.875 112.875 174.875 179.875 251.875)) (1.5 (63.125 80.625 135.125 152.625)) (4.5 (63.125 80.625 135.125 152.625)) (4.0 (184.875)) (2.0 (184.875))))

	(foreach x (cdr edgegroup)
		(setq val (cadr x))
		(cond 
			((vl-every '(lambda (w) (<= (car r1) w (cdr r1))) val) ((setq eg1 (cons x eg1))))
			((vl-every '(lambda (w) (<= (car r2) w (cdr r2))) val) ((setq eg2 (cons x eg2))))
			(t ((setq eg3 (cons x eg3))))
		)
	)

 Any thoughts on where I've gone wrong? I suspect its something to do with the vl-every but every example I've checked seems to match that syntax so I'm a bit confused.

0 Likes
Accepted solutions (1)
623 Views
2 Replies
Replies (2)
Message 2 of 3

john.uhden
Mentor
Mentor
Accepted solution

@spencer.robertsU8DLN 

I don't know how you even ran it.

You have ((setq eg1 ...)) and ((setq eg2 ...))

meaning extra left and right parentheses.

Try this...

(defun c:test ()
  (setq r1 (cons 0 101.35))
  (setq r2 (cons 152.025 253.375))
  (setq eg1 nil eg2 nil eg3 nil)
  (setq edgegroup 
   '(("OFFSET" nil)
     (3.0 (1.375 7.375 35.875 40.875 91.875 102.875 107.875 112.875 174.875 179.875 251.875))
     (1.5 (63.125 80.625 135.125 152.625))
     (4.5 (63.125 80.625 135.125 152.625))
     (4.0 (184.875))
     (2.0 (184.875))
    )
  )
  (foreach x (cdr edgegroup)
    (setq val (cadr x))
    (cond 
      ((vl-every '(lambda (w) (<= (car r1) w (cdr r1))) val) (setq eg1 (cons x eg1)))
      ((vl-every '(lambda (w) (<= (car r2) w (cdr r2))) val) (setq eg2 (cons x eg2)))
      (t (setq eg3 (cons x eg3)))
    )
  )
  (princ "\neg1 = ")(princ eg1)
  (princ "\neg2 = ")(princ eg2)
  (princ "\neg3 = ")(princ eg3)
  (princ)
)

BTW, I would classify your use of vl-every as excellent.  I can never get the *#*&$ function to work for me.

John F. Uhden

0 Likes
Message 3 of 3

spencer.robertsU8DLN
Enthusiast
Enthusiast

Thank you! your suggested tweaks work perfectly. I was running myself ragged to trying to figure this one out, figures it was something silly like that.

0 Likes