Jump to content

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

Link to comment
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[0]]
        For $i = 1 to $Array[0]
            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

Link to comment
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

Link to comment
Share on other sites

  • Developers

#include<array.au3>
Dim $array[5]
$array[0] = 2
$array[1] = 6
$array[2] = 5
$array[3] = 12
$array[4] = 1
;
$min = _ArrayMin($array)
ConsoleWrite('$min = ' & $min & @LF)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...