Jump to content

find min and max value


mien

Recommended Posts

Depends on the format of the text file, but your code would probably look something like this:

 

(setq fileID "path\\filename.txt" linLst '())
(setq file (open fileID "R"))
(while (setq line (read-line file))
 (setq linLst (cons line linLst))
 )
(close file)

(setq maxNum (apply 'max linLst))
(setq minNum (apply 'min linLst))

Link to comment
Share on other sites

May need a few "distof"'s in there...

 

(if (setq file (getfiled "Select File" "" "txt" )
 (progn
   (setq file (open file "r"))
   (while (setq nl (read-line file))
     (setq lst (cons (distof nl) lst)))
   (close file)

   (apply 'max lst)
   (apply 'min lst)

Link to comment
Share on other sites

  • 10 years later...

And how to do the same with the list? Choose a coordinate from the list with a minimum and maximum value of Z.

List:

((76751.7 53561.3 765.102) (76751.7 53561.3 765.102) (76758.6 53565.1 767.424) (76750.7 53565.9 766.755))

minimum:

(76751.7 53561.3 765.102)

maximum

(76758.6 53565.1 767.424)

 

Link to comment
Share on other sites

You can sort a list on the z value caddr. look at 1st entry and last

 

;sort on x

(setq lst
 (vl-sort co-ordsxy
 (function (lambda (e1 e2)
 (< (car e1) (car e2)))
 )
 )
)

 

;sort on z

(setq lst
 (vl-sort co-ordsxy
 (function (lambda (e1 e2)
 (< (caddr e1) (caddr e2)))
 )
 )
)

 

(setq minz (nth 0 lst))

(setq maxz (nth (- (length lst) 1) lst))

  • Like 1
Link to comment
Share on other sites

Sorting is inefficient when only an extremum is required.

 

You may find existing functions covering this operation here and a function specific to finding the minimum/maximum Z-coordinate here.

  • Like 1
Link to comment
Share on other sites

9 hours ago, Lee Mac said:

Sorting is inefficient when only an extremum is required.

 

You may find existing functions covering this operation here and a function specific to finding the minimum/maximum Z-coordinate here.

Thank you very much, Lee Mac.

Link to comment
Share on other sites

Nice Lee as usual did not even think about iterate over list check max min. Easy to add a couple more conds and make a library routine for min max X Y Z

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...