Help with finding largest of 5 random variables

Go to solution Solved by jdelaney,

Recommended Posts

Hey guys, I have a problem with finding the biggest number of 5 (random) variables in these 2 different situations.

Situation 1

\$number1= 5

\$number2= 7

\$number3= 6

\$number4= 8

\$number5= 2

It does NOT have to tell me that 8 was the biggest, instead I want it to tell me that \$number4 was the largest number.

Situation 2

\$number1= 7

\$number2= 7

\$number3= 3

\$number4= 7

\$number5= 7

In this situation, I just want him to tell me that \$number1 was the largest number, and forget about \$number 2, 4 and 5. It has to pick out the largest number while choosing in "order". So if \$number1 was 5, it had to tell me that \$number2 was the biggest and forget about \$number4 and \$number 5.

Share on other sites

Hi laughms

you should use arrays instead of single independant variables (is it allowed? if so)
take a look in the help to this functions:
_ArrayMax
_ArrayMaxIndex
_ArrayUnique

Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share on other sites

Thanks. I know about _ArrayMax, but if I put those variables as arrays, it will just show me the biggest number. It won't show me which \$x was the biggest. Is there a way without arrays?

Share on other sites

ArrayMaxIndex will tell you which element holds the highest value.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share on other sites

• Solution

Here ya go:

```\$number1= 5
\$number2= 7
\$number3= 6
\$number4= 8
\$number5= 2

\$iCountMax = 0
\$iMax = 0
For \$i = 1 To 5
\$iNumber = Eval("number" & \$i)
If \$iNumber > \$iMax Then
\$iMax = \$iNumber
\$iCountMax = \$i
EndIf
Next
ConsoleWrite(\$iMax & @CRLF)
ConsoleWrite("Largest Var = \$number" & \$iCountMax & "...value=" & Eval("number" & \$iCountMax) & @CRLF)```

Or, dynamically, for unknown count of vars, or if you ever add more in and don't want to rewrite the code:

```\$number1= 5
\$number2= 7
\$number3= 6
\$number4= 8
\$number5= 2

\$iCountMax = 0
\$iMax = 0
\$i = 1
While True
\$iNumber = Eval("number" & \$i)
If @error Then ExitLoop
If \$iNumber > \$iMax Then
\$iMax = \$iNumber
\$iCountMax = \$i
EndIf
\$i+=1
Wend
ConsoleWrite(\$iMax & @CRLF)
ConsoleWrite("Largest Var = \$number" & \$iCountMax & "...value=" & Eval("number" & \$iCountMax) & @CRLF)```

both will work for both situations

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Share on other sites

Here ya go:

```\$number1= 5
\$number2= 7
\$number3= 6
\$number4= 8
\$number5= 2

\$iMax = 0
For \$i = 1 To 5
\$iNumber = Eval("number" & \$i)
If \$iNumber > \$iMax Then
\$iMax = \$iNumber
\$iCountMax = \$i
EndIf
Next
ConsoleWrite(\$iMax & @CRLF)
ConsoleWrite("Largest Var = \$number" & \$iCountMax & "...value=" & Eval("number" & \$iCountMax) & @CRLF)```

what position was \$iMax?

Edit:

I was testing on your first version before you editing (now ok, sorry)

Edited by PincoPanco

Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share on other sites

\$iMax is the value of the variable...\$iCountMax is the integer appended at the end of the variable name.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Share on other sites

\$iMax is the value of the variable...\$iCountMax is the integer appended at the end of the variable name.

yes, thanks

just a curiosity, is there a way to have the name of the variable instead of the content? the inverse of eval ()

for example: VarName(\$iVariable) --> should return "\$iVariable" instead of it's content...

Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share on other sites

Not to my knowledge, except for what I have in the second consolewrite.

You would need to assign the variable to a variable, so even if there was such a function, it would return the name of the variable housing it, and not the actual \$number#

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Share on other sites

Not to my knowledge, except for what I have in the second consolewrite.

You would need to assign the variable to a variable, so even if there was such a function, it would return the name of the variable housing it, and not the actual \$number#

i mean somethink like this:

```Local \$iMyVal = 5
ConsoleWrite(VarName(\$iMyVal) & @CRLF)

Func VarName(ByRef \$Var)
\$result = "The referred variable is " & "?? how ??" ; should return "\$iMyVal"
Return \$result
EndFunc   ;==>VarName```

EDIT:

.... it does not matter, forget it....

excuse me @laughms

Edited by PincoPanco

Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share on other sites

@everybody. Thanks for helping!

Create an account

Register a new account