aridzv Posted May 4, 2025 Posted May 4, 2025 (edited) Hi. How do I get the varaible name that holds the resault of "max" function? Example: (setq var1 1) (setq var2 8) (setq var3 2) (setq lst (list var1 var2 var3)) (setq rsltmx (apply 'max lst)) ;;the return is 8 ;;I need to get "var2". I'm using this: (setq i 1) (foreach cnx lst (if (= cnx rsltmx) (progn (setq titlever (STRCAT "VAR" (rtos i 2 0))) (setq ix i) );progn );if (setq i (1+ i)) );foreach This requires that all variables have the same prefix and end with consecutive numbers. I'm looking for a more "direct" way that will not depends on fixing the names of the variables and their position in the list by their suffix. any ideas? thanks, aridzv. Edited May 4, 2025 by aridzv Quote
GLAVCVS Posted May 4, 2025 Posted May 4, 2025 (edited) Hi ' (setq var (max var1 var2 var3... varn)) ' Edited May 4, 2025 by GLAVCVS Quote
aridzv Posted May 4, 2025 Author Posted May 4, 2025 @GLAVCVS it gives the max value, not the var name that holds that value... thanks, aridzv Quote
GLAVCVS Posted May 4, 2025 Posted May 4, 2025 (nth (vl-position (max var1 var2 var3 var4) (list var1 var2 var3 var4) (list "var1" "var2" "var3" "var4")) This is one of the possible ways 1 Quote
GLAVCVS Posted May 4, 2025 Posted May 4, 2025 Another way... (strcat "var" (itoa (1+ (vl-position (max var1 var2 var3 var4) (list var1 var2 var3 var4)))) Quote
aridzv Posted May 4, 2025 Author Posted May 4, 2025 (edited) @GLAVCVS Thanks!! your first one worked. here: (setq as 1) (setq df 8) (setq io 3) (setq varx (nth (vl-position (max as df io) (list as df io)) (list "as" "df" "io")))) (The second is the same principle as my function) thanks, aridzv. Edited May 4, 2025 by aridzv Quote
Recommended Posts
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.