Jump to content


Photo

Comparing Variable Values


  • Please log in to reply
5 replies to this topic

#1 Lithodora

Lithodora
  • Members
  • 30 posts

Posted 20 May 2010 - 12:10 PM

Let's say I have 6 variables; var1 var2 var3 var4 var5 var6
Each has an unknown number set as it's value. I need to know which of the 6 has the largest number. I need a function to give the response of the name of the variable.

It seems simple but I've been up overnight working on stuff and I can't seem to wrap my head around it. Any help would be great.

#2 frescalus

frescalus
  • Members
  • 163 posts

Posted 20 May 2010 - 12:34 PM

ComparisonEx:
TempVar = 0
VarCount = 6

Loop, %VarCount%
	Random, Var%A_Index%, 1, 1000

Loop, %VarCount%
	If(Var%A_Index% > TempVar)
	{
		TempVar := Var%A_Index%
		VarName = Var%A_Index%
	}

MsgBox, Var1: %Var1% - Var2: %Var2% - Var3: %Var3% - Var4: %Var4% - Var5: %Var5% - Var6: %Var6%`n%VarName% has the highest Number: %TempVar%
Return

I decided to do it in 2 loops instead of 1 so I can have the numbers pre-generated instead of comparing them on the fly...

#3 alexys

alexys
  • Guests

Posted 20 May 2010 - 12:47 PM

var1 = 65

var2 = 132

var3 = 34

var4 = 48

var5 = 95

var6 = 83



loop, 6 {

  if (var%A_Index% > maxvalue) {

     maxvalue := var%A_Index%

     varname := "var" . A_Index

  }

}

msgbox, %maxvalue% is in %varname%


#4 frescalus

frescalus
  • Members
  • 163 posts

Posted 20 May 2010 - 12:56 PM

I added the extra line to get the variable name, but the rest of what I had is the exact same method... only difference is I randomly generated the numbers....

You don't have to do varname := "var" . A_Index though... can just do varname = var%A_Index% has the same affect

#5 tank

tank
  • Members
  • 4136 posts

Posted 20 May 2010 - 01:08 PM

shorter more to the point
largest(list_of_vars)
{
	global
	loop,parse,list_of_vars,`n
	{
		largestvar := %A_LoopField% > largest ? A_LoopField : largestvar ;;determin the largest var hereDe
		largest := %A_LoopField% > largest ? %A_LoopField% : largest ;; set the largest value for the next comparison
	}
	Return largestvar ; "=" largest ;; your answer is here
}
list_of_vars := "ahkvar`nvarinahk`nvariablewithnumber1`nan_ahkvar`nahk_var`nlastvar"
;; in the real world these vars will already have vars you could comment this loop out
loop,parse,list_of_vars,`n
    Random, %A_LoopField%, 1, 1000 
msgbox % largest(list_of_vars)
Edit mine said shorter and more to the point there were several posts saved just before mine

#6 sinkfaze

sinkfaze
  • Moderators
  • 6169 posts

Posted 20 May 2010 - 02:46 PM

I thought he had meant each variable has its own unknown group of numbers, a lot of work for nothing :roll: . Anyway, here's how I handled it:

var1=23,26,29,11,9
var2=5,25,21,2,17
var3=27,6,20,12,18
var4=28,16,13,8,19
var5=3,15,1,24,30
var6=14,4,22,7,10
[color=#770000]Loop[/color] 6 { ; finds the largest number in each var and saves only that number to var
	[color=#770000]Sort[/color], var%[color=#FF8242]A_Index[/color]%, N R D,
	var%[color=#FF8242]A_Index[/color]% :=	[color=#770000]SubStr[/color](var%[color=#FF8242]A_Index[/color]%,1,[color=#770000]InStr[/color](var%[color=#FF8242]A_Index[/color]%,[color=#999933]","[/color])-1)
}
[color=#770000]loop[/color], 6
	[color=#770000]if[/color]	(var%[color=#FF8242]A_Index[/color]% > max) 
		max :=	var%[color=#FF8242]A_Index[/color]%, varname :=	[color=#999933]"var"[/color] . [color=#FF8242]A_Index[/color] 
[color=#770000]MsgBox[/color], %max% is in %varname%
[color=#770000]return[/color]