Code:
;; KixGenerated: 2006/06/20 14:40:51
; Declare variables
Dim $Array[49], $Result[4,1], $Result2[4,1]
Dim $, $Small, $Position, $Pass, $Upper, $Lower, $Max
; Initialize the array with random values
; In production, this data would come from an external source
; simulate two equal values in the "smallest" range (pos 4 & 24 in this example)
$Array[0] = 034897549
$Array[1] = 46743984
$Array[2] = 246
$Array[3] = 43089743
$Array[4] = 35
$Array[5] = 490
$Array[6] = 9438
$Array[7] = 94378
$Array[8] = 436
$Array[9] = 4387
$Array[10] = 4307
$Array[11] = 08349
$Array[12] = 451
$Array[13] = 6165
$Array[14] = 65168
$Array[15] = 3573
$Array[16] = 357
$Array[17] = 619
$Array[18] = 38767
$Array[19] = 327
$Array[20] = 981
$Array[21] = 387
$Array[22] = 875
$Array[23] = 4657
$Array[24] = 35
$Array[25] = 78
$Array[26] = 8218
$Array[27] = 486
$Array[28] = 48941
$Array[29] = 654
$Array[30] = 684
$Array[31] = 98
$Array[32] = 896
$Array[33] = 985
$Array[34] = 12556
$Array[35] = 5789
$Array[36] = 84259
$Array[37] = 84685
$Array[38] = 965
$Array[39] = 745
$Array[40] = 365
$Array[41] = 125
$Array[42] = 985
$Array[43] = 852
$Array[44] = 3587853
$Array[45] = 2256
$Array[46] = 652
$Array[47] = 26354
$Array[48] = 52
$Array[49] = 96521
; Actual logic starts here -------------------------------
; Find the largest value in the array
$Max = 0
For $ = 0 to 49
If $Array[$] > $Max
$Max = $Array[$]
EndIf
Next
; Process the array looking for the 5 lowest values and their positions
$Lower = -1 ; Lower value limit
For $Pass = 0 to 4 ; 5 iterations
$Upper = $Max ; max value
$Position = 0 ; init position
$Small = 0 ; init value
For $ = 0 to 49
; Is the value between the lower and upper limits?
If $Array[$] > $Lower And $Array[$] < $Upper
$Small = $Array[$] ; set small value
$Position = $ ; record the position
$Upper = $Small ; reset the upper limit, looking for something smaller
EndIf
Next
$Result[$Pass,0] = $Small ; Value of smallest element
$Result[$Pass,1] = $Position ; position of smallest element
$Lower = $Small ; set the new lower limit
Next
; Display the intermediate results - these are the 5 smallest values and their array positions
? '5 smallest values / positions' ?
For $Pass = 0 to 4
$Result[$Pass,0] ' / ' $Result[$Pass,1] ?
Next
; Original data could have duplicate values - This will locate the 5 smallest values (with dups)
; and identify their array postions
$Pass = 0
$Count = 0
While $Pass < 5 And $Count < 5
For $ = 0 to 49
If $Array[$] = $Result[$Pass,0]
$Result2[$Count, 0] = $Array[$]
$Result2[$Count, 1] = $
$Count = $Count + 1
EndIf
Next
$Pass = $Pass + 1
Loop
; Display the final results
? 'Contents of 5 smallest elements' ?
For $Pass = 0 to 4
$Result2[$Pass,0] ' / ' $Result2[$Pass,1] ?
Next
Of course, I'd tweak this a bit to use UBound instead of hard coded array values, and maybe use $MaxRet to allow a variable number of items to be returned.
If converting to a UDF, you could either return the Result array to get the 5 smallest values, or Result2, to get the 5 smallest elements.
Glenn