Code: Select all
point := 1.5
loop
{
random,ran,-1.5,2.5
random,ran2,1~100
var .= ran "ID" ran2"`n"
if a_index = 7
break
}
; The above loop statement is not important. Only numbers that accumulate in var are important.
; Then, in the "var" variable, find the number closest to the "point" variable and try to find the corresponding ID.
loop,parse,var,`n
{
stringgetpos,var2,a_loopfield,ID
stringleft,var3,a_loopfield,%var2%
var4 .= var3 "`n"
}
; Only the decimal point values in the var variable are stored in var4.
for i, v in StrSplit(RTrim(var4, "`n"), "`n", "`n")
{
if(!smallest_diff or (Abs(point - v) < smallest_diff))
{
smallest_diff := Abs(point - v)
best_candidate := v
}
}
; Then find the closest match to the "point" variable in the "var4" variable.
loop,parse,var,`n
{
checkvar2 := a_loopfield
ifinstring,checkvar2,%best_candidate%
{
stringgetpos,checkvar2,checkvar2,ID
checkvar2 := checkvar2+2
stringtrimleft,checkvar3,a_loopfield,%checkvar2%
break
}
}
; Now that have the closest number get the id.
I would like to get help from the forum users and fix it with faster and optimized code.