Jump to content

Find maximum number in a selection


motee-z

Recommended Posts

I need a lisp that can find maximum value number in a selection contain numbers and text sting

any help will be appreciated

Link to comment
Share on other sites

convert strings to numbers with atof

add it to a list

use vl-sort > on list

output first number in list

 

;;----------------------------------------------------------------------------;;
;;List Maximum Value
(defun C:MValue (/ ss txt lst)
  (if (setq ss (ssget '((0 . "*TEXT"))))
    (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq value (atof (cdr (assoc 1 (entget txt)))))
      (setq lst (cons value lst))
      (setq lst (vl-sort lst '>))
    )
  )
  (prompt (strcat "\nMaximum Value: " (rtos (car lst) 2)))
  (princ)
)

 

--Edit

 

If your strings contain letters and numbers might have to do a little more before atof

 

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

Another way:

(defun c:foo (/ ss)
  (if (setq ss (ssget '((0 . "*TEXT"))))
    (print (apply 'max
		  (mapcar '(lambda (x) (atof (cdr (assoc 1 (entget x)))))
			  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
		  )
	   )
    )
  )
  (princ)
)

 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
On 14/07/2022 at 00:37, mhupp said:

convert strings to numbers with atof

add it to a list

use vl-sort > on list

output first number in list

 

;;----------------------------------------------------------------------------;;
;;List Maximum Value
(defun C:MValue (/ ss txt lst)
  (if (setq ss (ssget '((0 . "*TEXT"))))
    (foreach txt (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq value (atof (cdr (assoc 1 (entget txt)))))
      (setq lst (cons value lst))
      (setq lst (vl-sort lst '>))
    )
  )
  (prompt (strcat "\nMaximum Value: " (rtos (car lst) 2)))
  (princ)
)

 

--Edit

 

If your strings contain letters and numbers might have to do a little more before atof

 

 

is there any possibility to keep highest vale and remove remaining? please suggest,.

Link to comment
Share on other sites

If you have all the values as a list, largest first you could try a very simple from MHUPPS example

 

(setq lst (car lst))

 

of

(setq lst (nth 0 lst))

 

Both set the variable lst to the first value in that list and deletes all the others

 

I think RonJonp offered a solution that does the same ,  Replace (print with (setq maxvalue

  • Like 1
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...