如何在AutoIt中对2D数组的列序列进行排序

时间:2019-02-07 12:39:39

标签: autoit array-column

摘要:

我正在寻找一种通过AutoIt中的列序列对2D(多维)数组排序的正确方法。我对AutoIt非常熟悉,并且有一个_ArraySort函数,该函数提供按列排序的结果,从而导致对行进行排序。但是我需要的是按列方向(按字母顺序)对列顺序/序列进行排序。

背景和目标:

对于我创建的测试案例报告(一个HTML文件),我需要在其中显示一个表。该表应包含测试用例结果的一部分数据。

我已经实现的是:

我将CSV文件读入数组(通过定义的分隔符),这不是问题。我可以为行序列排序2D数组,并且报表的解析和转换为HTML结构也不是问题。

但是我要实现的目标显示在底部的示例表中。请注意,该示例仅包含示例数据。实际的数组内容将作为测试用例结果在数组中具有更多的列和行。但这应该足以理解这个想法。

代码:

这只是示例数组数据,如下所示:

Global $a2dData[6][6]

$a2dData[0][0] = 'fruits'
$a2dData[0][1] = 'IT manufactures'
$a2dData[0][2] = 'painters'
$a2dData[0][3] = 'music genres'
$a2dData[0][4] = 'days'

$a2dData[1][0] = 'apple'
$a2dData[1][1] = 'Google'
$a2dData[1][2] = 'Picasso'
$a2dData[1][3] = 'Jazz'
$a2dData[1][4] = 'Monday'

$a2dData[2][0] = 'Orange'
$a2dData[2][1] = 'Microsoft'
$a2dData[2][2] = 'Van Gogh'
$a2dData[2][3] = 'Blues'
$a2dData[2][4] = 'Thursday'

$a2dData[3][0] = 'Pear'
$a2dData[3][1] = ''
$a2dData[3][2] = 'Da Vinci'
$a2dData[3][3] = 'Hip Hop'
$a2dData[3][4] = 'Friday'

$a2dData[4][0] = ''
$a2dData[4][1] = 'Apple'
$a2dData[4][2] = 'Monet'
$a2dData[4][3] = 'Rap'
$a2dData[4][4] = 'Sunday'

$a2dData[5][0] = 'Cherry'
$a2dData[5][1] = 'Adobe'
$a2dData[5][2] = 'Michelangelo'
$a2dData[5][3] = 'Classic'
$a2dData[5][4] = ''

_ArrayDisplay( $a2dData )

现在,如何在AutoIt中对2D数组的列序列进行排序?

对列序列进行排序之前的实际数组数据:

| fruits | IT manufactures | painters     | music genres | days     |
| apple  | Google          | Picasso      | Jazz         | Monday   |
| Orange | Microsoft       | Van Gogh     | Blues        | Thursday |
| Pear   |                 | Da Vinci     | Hip Hop      | Friday   |
|        | Apple           | Monet        | Rap          | Sunday   |
| Cherry | Adobe           | Michelangelo | Classic      |          |

对列序列进行排序后的预期数组数据:

| days     | fruits | IT manufactures | music genres | painters     |
| Monday   | apple  | Google          | Jazz         | Picasso      |
| Thursday | Orange | Microsoft       | Blues        | Van Gogh     |
| Friday   | Pear   |                 | Hip Hop      | Da Vinci     |
| Sunday   |        | Apple           | Rap          | Monet        |
|          | Cherry | Adobe           | Classic      | Michelangelo |

已经有一个函数可以执行此操作吗?还是我必须自己做一个?

2 个答案:

答案 0 :(得分:2)

这符合您的期望吗?

#include <Array.au3>
Global $a2dData[6][5]

$a2dData[0][0] = 'fruits'
$a2dData[0][1] = 'IT manufactures'
$a2dData[0][2] = 'painters'
$a2dData[0][3] = 'music genres'
$a2dData[0][4] = 'days'

$a2dData[1][0] = 'apple'
$a2dData[1][1] = 'Google'
$a2dData[1][2] = 'Picasso'
$a2dData[1][3] = 'Jazz'
$a2dData[1][4] = 'Monday'

$a2dData[2][0] = 'Orange'
$a2dData[2][1] = 'Microsoft'
$a2dData[2][2] = 'Van Gogh'
$a2dData[2][3] = 'Blues'
$a2dData[2][4] = 'Thursday'

$a2dData[3][0] = 'Pear'
$a2dData[3][1] = ''
$a2dData[3][2] = 'Da Vinci'
$a2dData[3][3] = 'Hip Hop'
$a2dData[3][4] = 'Friday'

$a2dData[4][0] = ''
$a2dData[4][1] = 'Apple'
$a2dData[4][2] = 'Monet'
$a2dData[4][3] = 'Rap'
$a2dData[4][4] = 'Sunday'

$a2dData[5][0] = 'Cherry'
$a2dData[5][1] = 'Adobe'
$a2dData[5][2] = 'Michelangelo'
$a2dData[5][3] = 'Classic'
$a2dData[5][4] = ''

_ArrayDisplay($a2dData, 'BEFORE')
_ArrayTranspose($a2dData)
_ArraySort($a2dData)
_ArrayTranspose($a2dData)
_ArrayDisplay($a2dData, 'AFTER' )

这么久 兆

答案 1 :(得分:0)

是:

谢谢,异种生物学家的解决方案完全符合我的期望。但是我不能这么说。

其他解决方案:

同时,我也自己解决了它(但不是以这种优雅的方式):

Global $a2dData[6][6]

$a2dData[0][0] = 'fruits'
$a2dData[0][1] = 'IT manufactures'
$a2dData[0][2] = 'painters'
$a2dData[0][3] = 'music genres'
$a2dData[0][4] = 'days'

$a2dData[1][0] = 'apple'
$a2dData[1][1] = 'Google'
$a2dData[1][2] = 'Picasso'
$a2dData[1][3] = 'Jazz'
$a2dData[1][4] = 'Monday'

$a2dData[2][0] = 'Orange'
$a2dData[2][1] = 'Microsoft'
$a2dData[2][2] = 'Van Gogh'
$a2dData[2][3] = 'Blues'
$a2dData[2][4] = 'Thursday'

$a2dData[3][0] = 'Pear'
$a2dData[3][1] = ''
$a2dData[3][2] = 'Da Vinci'
$a2dData[3][3] = 'Hip Hop'
$a2dData[3][4] = 'Friday'

$a2dData[4][0] = ''
$a2dData[4][1] = 'Apple'
$a2dData[4][2] = 'Monet'
$a2dData[4][3] = 'Rap'
$a2dData[4][4] = 'Sunday'

$a2dData[5][0] = 'Cherry'
$a2dData[5][1] = 'Adobe'
$a2dData[5][2] = 'Michelangelo'
$a2dData[5][3] = 'Classic'
$a2dData[5][4] = ''

Func _arraySortColumnSequence( $a2dArray, $iAscOrDesc )
    Local $aJustColumns[1]
    For $i = 0 To UBound( $a2dArray, 2 ) - 2 Step 1
        _ArrayAdd( $aJustColumns, $a2dArray[0][$i] )
    Next

    _ArrayDelete( $aJustColumns, 0 )
    _ArraySort( $aJustColumns, $iAscOrDesc )

    Local $aNew2dArray[UBound( $a2dArray, 1 )][UBound( $a2dArray, 2 )]
    For $i = 0 To UBound( $aJustColumns ) - 1 Step 1
        For $j = 0 To UBound( $aJustColumns ) - 1 Step 1
            If $a2dArray[0][$j] = $aJustColumns[$i] Then
                For $k = 0 To UBound( $a2dArray, 1 ) - 1 Step 1
                    $aNew2dArray[$k][$i] = $a2dArray[$k][$j]
                Next
            EndIf
        Next
    Next

    Return $aNew2dArray
EndFunc


_ArrayDisplay( $a2dData, 'before' )

Global $a2dDataRes = _arraySortColumnSequence( $a2dData, 0 )

_ArrayDisplay( $a2dDataRes, 'after' )