AutoIt _ArraySort(..)函数未正确排序

时间:2016-09-09 16:53:15

标签: arrays sorting quicksort autoit

我正在处理一个将接收文本文件的AutoIt脚本。文本文件由一系列5行组的测试结果组成。此脚本将遍历文件并输出每个组的中位数分数。分数显示在第11个" "之后。例如,使用此示例文件:

a,a,a,a,a,a,a,a,a,a,100.2
a,a,a,a,a,a,a,a,a,a,300.2
a,a,a,a,a,a,a,a,a,a,160.2
a,a,a,a,a,a,a,a,a,a,301.2
a,a,a,a,a,a,a,a,a,a,161.2

b,b,b,b,b,b,b,b,b,b,110.5
b,b,b,b,b,b,b,b,b,b,87.5
b,b,b,b,b,b,b,b,b,b,89.5
b,b,b,b,b,b,b,b,b,b,190.5
b,b,b,b,b,b,b,b,b,b,170.5

c,c,c,c,c,c,c,c,c,c,90.2
c,c,c,c,c,c,c,c,c,c,190.2
c,c,c,c,c,c,c,c,c,c,40.2
c,c,c,c,c,c,c,c,c,c,20.2
c,c,c,c,c,c,c,c,c,c,80.2

我预计a,b和c组的中位数分别为161.2,110.5和80.2。但是,输出结果如下:

median is = 161.2 ----- sorted array : 100.2 , 160.2 , 161.2 , 300.2 , 301.2
median is = 190.5 ----- sorted array : 110.5 , 170.5 , 190.5 , 87.5 , 89.5
median is = 40.2 ----- sorted array : 190.2 , 20.2 , 40.2 , 80.2 , 90.2

第一组正确排序,但之后不再对数组进行排序。这是尽管在输出之前直接调用_ArraySort(...)。知道为什么会这样吗?

代码如下:

Func CondenseResults()

    $size = 5   ;temporary, variable to be passed on from other function

    $gameDetail = ""
    $rawResult = "D:\RTG_Benchmark\results\results.txt"
    local $openResults = FileOpen("D:\RTG_Benchmark\results\results2.txt", 1)
    Local $i = 1
    Local $j = 0
    Local $spaceCount = 0
    Local $score[$size]
    Local $runMultiplier[2]     ; 0 = previous, 1 = current
    Local $subString

    Do
        $resultLine = FileReadLine($rawResult, $i)      ; read line from raw result
        $subString =  StringSplit($resultLine,",")  ; split
        If StringLen($resultLine) = 0 Then
            $spaceCount += 1
            $j = 0
            FileWriteLine ( $openResults, "" & @CRLF)
        Else
            $gameDetail = $subString[1] & $subString[2] & $subString[3] & $subString[4] & $subString[5] & $subString[6] & $subString[7] & $subString[8] & $subString[9] & $subString[10]        ; substring 1 - 10
            $runMultiplier[1] = $gameDetail
            If $i = 1 Then      ;
                $score[$j] = $subString[11]
            Else
                If  $runMultiplier[1] <> $runMultiplier[0] Then
                    $j  = 0
                    $score[$j] = $subString[11]
                Else
                    $j += 1
                    If $j >= $size Then
                        $j = 0
                    EndIf
                    $score[$j] = $subString[11]
                EndIf

                $resultLine = FileReadLine($rawResult, $i+1)
                $subString =  StringSplit($resultLine,",")  ; split

                If StringLen($resultLine) = 0 Or $gameDetail <> $subString[1] & $subString[2] & $subString[3] & $subString[4] & $subString[5] & $subString[6] & $subString[7] & $subString[8] & $subString[9] & $subString[10] Then

                        _ArraySort($score, 0,0,0,0,1)
                        ConsoleWrite("median is = " & ($score[$size/2]  & " ----- sorted array : " & $score[0]  & " , " & $score[1]  & " , " & $score[2] & " , " & $score[3] & " , " & $score[4]& @CRLF))
                        $j  = 0
                EndIf


            EndIf
        EndIf
        $i += 1
        $runMultiplier[0] = $runMultiplier[1]
    Until $i >=_FileCountLines($rawResult)

    $j += 1
    $score[$j] = $subString[11]
    _ArraySort($score)
    ConsoleWrite("median is =" & ($score[$size/2]  & " ----- sorted array : " & $score[0]  & " , " & $score[1]  & " , " & $score[2] & " , " & $score[3] & " , " & $score[4]& @CRLF))


    FileClose($openResults)
    sleep(100000)
    Return 0
EndFunc

1 个答案:

答案 0 :(得分:1)

我发现了这个问题。 AutoIt将数字视为字符串,并且从左到右一次将它排序为一位数。

Number()函数解决了这个问题。