# Least Variable

## Recommended Posts

Is there a simple way to determine which variable has the least amount...

For example lets say I have 5 variables:

\$a = 2

\$b = 6

\$c = 5

\$d = 12

\$e = 1

Is there some way to just have it return the name of the variable with the least amount, "\$e"

I can do this using several calculations but I was hoping someone had a simple way like a UDF or something.

Thanks,

Terry

##### Share on other sites

Well, here's a UDF that will return the lowest value in an array, but as far as return the name of the variable, given multiple variables, I don't know.

```Func _Least(\$Array)
Dim \$RetVal, \$i
If IsArray(\$Array) Then
\$RetVal = \$Array[\$Array]
For \$i = 1 to \$Array
If IsNumber(\$Array[\$i]) Then
If \$Array[\$i] < \$RetVal Then \$RetVal = \$Array[\$i]
EndIf
Next
Return \$RetVal
Else
SetError(1)
Return -1
EndIf
EndFunc```

Perhaps if you give some context to what the number represent, someone can help you with some code to do it more efficiently than using five separate variables. What do these variables represent?

Edited by c0deWorm

My UDFs: ExitCodes

##### Share on other sites

Perhaps if you give some context to what the number represent, someone can help you with some code to do it more efficiently than using five separate variables. What do these variables represent?

OK, well I'll try.

Basically I am running a bunch of calculations on different things. These calculations are captured in seperate variables. At the end of all the calculations I simply want to know which variable has the lowest value so I can do stuff with that.

I gave the exampel above of 5 variables with different amounts. I don't necessarily need the variable name back, but I would need some sort of indicator that that variable is the one so I could do some "If" statements, etc...

This isn't that hard to do with a small number of variables for example with 3 variables (remember this is very simplified):

```; If one is THE lowest
If \$COUNTA < \$COUNTB Then
If \$COUNTA < \$COUNTC Then
msgbox(0, "test", "A is the lowest, Use A")
\$low = 1
EndIf
EndIf
If \$COUNTB < \$COUNTA Then
If \$COUNTB < \$COUNTC Then
msgbox(0, "test", "B is the lowest, Use B")
\$low = 1
EndIf
EndIf
If \$COUNTC < \$COUNTA Then
If \$COUNTC < \$COUNTB Then
msgbox(0, "test", "C is the lowest, Use C")
\$low = 1
EndIf
EndIf

; If there's multiple low ones that are equal
; This section could be much better to maybe randomize the option it picks if two are equal
If \$LOW <> 1 Then
If \$COUNTA = \$COUNTB Then
msgbox(0, "test", "Use A")
EndIf
If \$COUNTA = \$COUNTC Then
msgbox(0, "test", "Use A")
EndIf
If \$COUNTB = \$COUNTC Then
msgbox(0, "test", "Use B")
EndIf
EndIf```

Any ideas for making this simple?

For example a function you can pass variables to and it shoots back which has the lowest value or if multiple ones are equal it randomly selects one of the lowest equal ones...

Thanks,

Terry

##### Share on other sites

• Developers

```#include<array.au3>
Dim \$array
\$array = 2
\$array = 6
\$array = 5
\$array = 12
\$array = 1
;
\$min = _ArrayMin(\$array)
ConsoleWrite('\$min = ' & \$min & @LF)```

Live for the present,
Dream of the future,
Learn from the past. ##### Share on other sites

I think you're stuck with putting the values into an array and using _ArrayMin(). Your code works, but gets exponentially larger with each variable you add.

My UDFs: ExitCodes

##### Share on other sites

I think you're stuck with putting the values into an array and using _ArrayMin(). Your code works, but gets exponentially larger with each variable you add.

OK, I think you're right.

I just thought I'd check to see if there was a better way.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...