# Get the nearest Value from an Array with a given number?

## Recommended Posts

Greetings AutoIT community!

I have a simple code here that get the exact value from a given Array, now my problem is what if the exact value does not exist? What would be the code to search the nearest value of "\$GivenNumber" from the said Array?

Thanks in Advance! ```DIM \$aRecords1

\$GivenNumber = 3

For \$x = 1 to \$aRecords1

If \$PP < \$GivenNumberThen

ElseIf \$PP = \$GivenNumberThen

MSGBOX(0,"Exact Value", \$PP)

Else

\$LOW = "NO"

EndIf

Next```
##### Share on other sites

```DIM \$aRecords1
\$GivenNumber = 3
\$sLast = ""
\$sCurrent = 999999999 ; more elegant ways are possible :)
\$iClosest = ""
For \$x = 1 to \$aRecords1
\$sLast = \$sCurrent
\$sCurrent = Abs ( \$aRecords1[\$x]- \$GivenNumber)
If \$sCurrent < \$sLast Then \$iClosest = \$x
Next```

this will work.

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

<autoit>

For \$x = 1 to \$aRecords1

If \$aRecords1[\$x] = \$GivenNumber Then

MsgBox('',\$aRecords1[\$x] & '=' & \$GivenNumber,'MATCH')

ExitLoop

EndIf

\$sLast = \$sCurrent

\$sCurrent = Abs ( \$aRecords1[\$x] - \$GivenNumber)

If \$sCurrent < \$sLast Then \$iClosest = \$aRecords1[\$x]

Next

</autoit>

PS i borrowed all the code from jdelaney

EDIT - updated index in first IF statement from 0 to \$x

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow." Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

oh. let me try

Ng1-Nf3 code first.

Edited by NearValue
##### Share on other sites

outside of the loop, add the msgbox:

`MsgBox(0,"closest",\$avArray1[\$iClosest])`

you are only setting the array variable which is closest, you then need to use that relative to the array to return the value

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

also, if you are creating the array yourself, then it is probably 0 based, so switch out the for loop with:

`For \$x = 0 To UBound( \$avArray1) - 1`
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

The Array values are below:

12.001

6.001

4.001

3

```\$GivenNumber = 1

\$sLast = ""
\$sCurrent = 12.001 ;max number to search
\$iClosest = ""

For \$x = 0 To UBound( \$avArray1) - 1

If \$avArray1[\$x] = \$GivenNumber Then
MsgBox('',\$avArray1[\$x] & '=' & \$GivenNumber,'MATCH')
ExitLoop
EndIf

\$sLast = \$sCurrent
\$sCurrent = Abs ( \$avArray1[\$x] - \$GivenNumber)

If \$sCurrent < \$sLast Then \$iClosest = \$avArray1[\$x]

Next

MsgBox(0,"closest",\$avArray1[\$iClosest])```

But the code shows 6.001 as the closest instead of 3? Can't find the bug here ##### Share on other sites

Here a small different approach.

```#include <Array.au3>
Global \$aNumbers
For \$i = 0 To UBound(\$aNumbers) - 1
\$aNumbers[\$i] = Round(Random(0, 500, 0), 2)
Next

ConsoleWrite(SearchNearest(\$aNumbers, 111.11) & @LF)
_ArrayDisplay(\$aNumbers)

Func SearchNearest(\$aArray, \$fNumber)
_ArraySort(\$aArray)
Local \$i = 0, \$ret, \$dx, \$dx_old = 0x7FFFFFF
While True
\$dx = Abs(\$aArray[\$i] - \$fNumber)
If \$dx_old >= \$dx Then
\$dx_old = \$dx
\$ret = \$i
Else
ExitLoop
EndIf
\$i += 1
WEnd
Return \$aArray[\$ret]
EndFunc```

Br,

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

also, if you are creating the array yourself, then it is probably 0 based, so switch out the for loop with:

`For \$x = 0 To UBound( \$avArray1) - 1`

Two thumbs up for this!
##### Share on other sites

```#include <Array.au3>
\$GivenNumber = 1
dim \$avArray1= [12.001,6.001,4.001]
\$sLast = ""
\$sCurrent = _ArrayMax ( \$avArray1 ) ;max number to search
\$iClosest = ""
For \$x = 0 To UBound( \$avArray1) - 1
If \$avArray1[\$x] = \$GivenNumber Then
\$iClosest = \$x
ExitLoop
EndIf
\$sLast = \$sCurrent
\$sCurrent = Abs ( \$avArray1[\$x] - \$GivenNumber)
If \$sCurrent < \$sLast Then \$iClosest = \$x
Next
MsgBox(0,"closest",\$avArray1[\$iClosest])```

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

```#include <Array.au3>
\$GivenNumber = 1
dim \$avArray1= [12.001,6.001,4.001]
\$sLast = ""
\$sCurrent = _ArrayMax ( \$avArray1 ) ;max number to search
\$iClosest = ""
For \$x = 0 To UBound( \$avArray1) - 1
If \$avArray1[\$x] = \$GivenNumber Then
\$iClosest = \$x
ExitLoop
EndIf
\$sLast = \$sCurrent
\$sCurrent = Abs ( \$avArray1[\$x] - \$GivenNumber)
If \$sCurrent < \$sLast Then \$iClosest = \$x
Next
MsgBox(0,"closest",\$avArray1[\$iClosest])```

Ayt! Good Game jdelaney! Woot!
##### Share on other sites

it works, but it's dirty, and not re-usable. @UEZ is a better approach, and you may then re-use it...all about modularity.

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

Code now works like a charm ^^^^^^^^^__________________^^^^^^^^^^^^

##### Share on other sites

I think that's right!

##### Share on other sites

Without #include files.

```Local \$GivenNum = 7
Local \$Array = ["zx", 4.001, 12, 6.001, "ab"]

Local \$iIndex = _ArrayGetClosestValue(\$Array, \$GivenNum)
MsgBox(0, "closest to " & \$GivenNum, "Number " & \$Array[\$iIndex] & " closest in array with index " & \$iIndex & ".")

; Returns: The array index whose difference between the array value and the given number is the lowest.
Func _ArrayGetClosestValue(ByRef \$aArray, \$GivenNumber)
Local \$Diff = Abs(\$aArray - \$GivenNumber)
Local \$iClosest = 0

For \$x = 1 To UBound(\$aArray) - 1
If (Number(\$aArray[\$x]) <> "") And (Abs(\$aArray[\$x] - \$GivenNumber) < \$Diff) Then
\$Diff = Abs(\$aArray[\$x] - \$GivenNumber)
\$iClosest = \$x
EndIf
Next
Return \$iClosest
EndFunc   ;==>_ArrayGetClosestValue```

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...