# Finding top 4 or 3 or 2 values in an array

## Recommended Posts

I have an array that gets updated each time data in entered

local \$array[12]=[1,9,11,5,1,12,10,16,8,1,9,3]

I need to know when when 4 of the elements have hit 10 or greater, so from the above I would like to be told that

indexes 2,5,6,7 have met the criteria as there are 4 elements of 10 or greater.

How do I perform a check each time data is entered to find the top 4 values? I can get my head around it. I will also be doing the same on the top 3 and top 2 but assume top 4 would be the strating point to understand it.

Edited by Phaser
##### Share on other sites

Do you just have to know the number of elements or the indices of the elements > 10?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

##### Share on other sites

No, I need to know that there are 4 of 10 or greater and the indices they are at, thats why I cant get my head around it

##### Share on other sites

You would need something like this:

```#include <array.au3>

Global \$iLimit = 10
Global \$iCount = 0
Global \$aInput[12] = [1,9,11,5,1,12,10,16,8,1,9,3]
Global \$aIndices[UBound(\$aInput)]

For \$i = 0 To UBound(\$aInput)-1
If \$aInput[\$i] >= \$iLimit Then
For \$j = 0 To \$iCount
If \$aIndices[\$j] = \$aInput[\$i] Then ContinueLoop
If \$aIndices[\$j] = "" Then
\$aIndices[\$j] = \$aInput[\$i]
\$iCount = \$iCount + 1
ExitLoop
EndIf
Next
Endif
Next
_ArrayDisplay(\$aIndices)
ConsoleWrite("# of indices >= " & \$iCount & @CRLF)```

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

##### Share on other sites

Many thanks water, I will see if I can work with that, thanks for your help

##### Share on other sites

Try this.

```Local \$aInput[12] = [1, 9, 11, 5, 1, 12, 10, 16, 8, 1, 9, 3]
Local \$iLimit = 10
Local \$iCount = 0
Local \$sIndices = ""

For \$i = 0 To UBound(\$aInput) - 1
If \$aInput[\$i] >= \$iLimit Then
\$iCount += 1
If \$iCount <= 4 Then
\$sIndices &= \$i & ","
Else
ExitLoop
EndIf
EndIf
Next

ConsoleWrite("The indices with values >= 10 are " & StringTrimRight(\$sIndices, 1) & "." & @CRLF)

#cs
Output is:-
The indices with values >= 10 are 2,5,6,7.
#ce```

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

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