Finding the smallest number

Finding the smallest number

Henrik_Lo
Collaborator Collaborator
1,940 Views
14 Replies
Message 1 of 15

Finding the smallest number

Henrik_Lo
Collaborator
Collaborator

HI
I’ll like to knot the smallest number form 4 different variables ,

but I need to keep track of the variable name.

 

Example.

Ba1 =3
Ba2 = 14
Ba3 = 40
Ba4 = 10
(min ba1 ba2 ba3 ba4)
Returns 3, but I need to know it is ba1

 

Regards

Henrik

0 Likes
1,941 Views
14 Replies
Replies (14)
Message 2 of 15

Sea-Haven
Mentor
Mentor

Use a double list ((4 bal1)(45 bal5)(12 bal3)……… sort on X you will need to look at set read and eval though to get the variable name its a lambda thing which I am not very good at.

(setq lst (list '("ba1" 1) '("bal2" 23) '("bal3" 12) '("bal4" 15)))

sort list on 2nd item cadr.

(set (read (car (nth 1 lst)))(cadr (nth 0 lst)))

 

 

0 Likes
Message 3 of 15

marko_ribar
Advisor
Advisor
(cond
  ( (= ba1 (min ba1 ba2 ba3 ba4))
    (setq tmp ba1)
  )
  ( (= ba2 (min ba1 ba2 ba3 ba4))
    (setq tmp ba2)
  )
  ( (= ba3 (min ba1 ba2 ba3 ba4))
    (setq tmp ba3)
  )
  ( (= ba4 (min ba1 ba2 ba3 ba4))
    (setq tmp ba4)
  )
)
...
do with tmp all like you wanted with ba* (min)
...
Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 4 of 15

cadffm
Consultant
Consultant

For a long list of variables

use vl-sort-i and vl-position 

Sebastian

0 Likes
Message 5 of 15

_gile
Consultant
Consultant

Hi,

You can try this:

 

(defun minSym (l)
  (nth (car (vl-sort-i (mapcar 'eval l) '<)) l)
)

 

 

(minSym '(ba1 ba2 ba3 ba4)) returns BA1.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 15

Sea-Haven
Mentor
Mentor

Nice idea , so rather than multiple conds with unknown number of variables could write a script (foreach val '(bal1 bal2.…..

 

Have to find it again send a string which is lisp of your cond to the command line as its repeated but with new BalX.

0 Likes
Message 7 of 15

Henrik_Lo
Collaborator
Collaborator

HI all

Tangs a lot, simply a fantastic forum, tangs for helping med out

 

Regards

Henrik

0 Likes
Message 8 of 15

_gile
Consultant
Consultant

You can also use a 'reduce' function.

 

(defun reduce (fun lst)
  (setq fun (eval fun))
  (while (cdr lst)
    (setq lst (cons (fun (car lst) (cadr lst)) (cddr lst)))
  )
  (car lst)
)

 

 

(reduce '(lambda (a b) (if (< (eval a) (eval b)) a b)) '(ba1 ba2 ba3 ba4)) return BA1



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 15

hak_vz
Advisor
Advisor
(defun set_min (lst)(mapcar 'set '(Ba1 Ba2 Ba3 Ba4)(vl-sort lst '<)))

(set_min '(40 30 20 10)) ->(10 20 30 40)

!ba1 -> 10
!ba2 -> 20
!ba3 -> 30
!ba4 -> 40 

This way ba1 will allways hold smallest value and ba4 will be largest

 

(defun set_min (lst)(mapcar 'set '(Ba1 Ba2 Ba3 Ba4)(vl-sort lst '<)))

(set_min '(40 30 20 10)) ->(10 20 30 40)

!ba1 -> 10
!ba2 -> 20
!ba3 -> 30
!ba4 -> 40 ​

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 10 of 15

hak_vz
Advisor
Advisor

Let say you have run some code and instead of checkin which of vars in minimal you can reasign them all in a range from ba1 to ba4

 

;let say this are results of some previous calculations
(setq ba1 50.2 ba2 30.3 ba3 70.4 ba4 10.4)
;we assign values to new list p
(setq p (list ba1 ba2 ba3 ba4))
; and reassign values to vars ba1 ..ba4
(set_min (list ba1 ba2 ba3 ba4))
!ba1 -> 10.4
!ba2 -> 30.3
!ba3 -> 50.2
!ba4 -> 70.4

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 11 of 15

marko_ribar
Advisor
Advisor

@marko_ribar wrote:

 

(cond
  ( (= ba1 (min ba1 ba2 ba3 ba4))
    (setq tmp ba1)
  )
  ( (= ba2 (min ba1 ba2 ba3 ba4))
    (setq tmp ba2)
  )
  ( (= ba3 (min ba1 ba2 ba3 ba4))
    (setq tmp ba3)
  )
  ( (= ba4 (min ba1 ba2 ba3 ba4))
    (setq tmp ba4)
  )
)
...
do with tmp all like you wanted with ba* (min)
...

 


Still I don' quite undertand what OP wants to achieve... Without (cond), why this couldn't all be simply :

(setq tmp (min ba1 ba2 ba3 ba4))

And 'tmp' could be used instead of any 'ba*'...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 12 of 15

Kent1Cooper
Consultant
Consultant

@marko_ribar wrote:

....


Still I don' quite undertand what OP wants to achieve... Without (cond), why this couldn't all be simply :

(setq tmp (min ba1 ba2 ba3 ba4))

....


I thought it was pretty clear in Message 1 -- they want to know which of the variable names  holds the smallest value, not what that smallest value is, which is what that expression returns.

Kent Cooper, AIA
0 Likes
Message 13 of 15

marko_ribar
Advisor
Advisor

@Kent1Cooper wrote:

@marko_ribar wrote:

....


Still I don' quite undertand what OP wants to achieve... Without (cond), why this couldn't all be simply :

(setq tmp (min ba1 ba2 ba3 ba4))

....


I thought it was pretty clear in Message 1 -- they want to know which of the variable names  holds the smallest value, not what that smallest value is, which is what that expression returns.


Then simply :

(setq tmp (min ba1 ba2 ba3 ba4))
(setq tmp
  (cond
    ( (= tmp ba1)
      "ba1"
    )
    ( (= tmp ba2)
      "ba2"
    )
    ( (= tmp ba3)
      "ba3"
    )
    ( (= tmp ba4)
      "ba4"
    )
  )
)
(princ tmp)
Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 14 of 15

marko_ribar
Advisor
Advisor

Another one more succinct...

 

 

(defun smallestvar ( lstvars / l minval minvar )
  (setq l (mapcar 'eval lstvars))
  (setq minval (apply 'min l))
  (setq minvar (vl-some '(lambda ( x ) (if (= (eval x) minval) x)) lstvars))
  minvar
)

(setq lstvars '(ba1 ba2 ba3 ba4))
(setq ba1 10.0 ba2 5.0 ba3 15.0 ba4 20.0)

(princ (smallestvar lstvars))
(princ)

 

HTH.

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 15 of 15

Kent1Cooper
Consultant
Consultant

@Henrik_Lo wrote:

....
I’ll like to knot the smallest number form 4 different variables ,

....


Would it always be 4 and only 4?  It appears some of the things suggested here so far depend on that, so you would need different versions for different numbers of variables, but others presumably could work with any number of them.

Kent Cooper, AIA
0 Likes