Swap 2D array's rows and columns

前端 未结 3 559
粉色の甜心
粉色の甜心 2020-12-22 02:45

I need to get columns from a CSV file into an array (multidimensional array or array of arrays). I used CSV.au3 which loads the rows just fine, but I need the c

3条回答
  •  生来不讨喜
    2020-12-22 03:20

    Here is CSV to 2D Array example. Credits to Malkey.

    #include 
    
    Opt("MustDeclareVars", 1)
    
    Dim $sCSV = '"010","2","03"' & @CRLF & _
            '"24","30","20"' & @CRLF & _
            '"txt","bla","toto"'
    
    Local $arr = _CsvToArray2D($sCSV)
    _ArrayDisplay($arr)
    
    ; Converts CSV format to a 2D array.
    Func _CsvToArray2D($sCSV)
        Local $aTmp = StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3)
        Local $NumCols[UBound($aTmp)]
        For $x = 0 To UBound($aTmp) - 1
            StringReplace($aTmp[$x], ",", ",")
            $NumCols[$x] = @extended + 1
        Next
        Local $Max = _ArrayMax($NumCols, 1)
    
        Dim $aArr[UBound($aTmp)][$Max]
    
        For $i = 0 To UBound($aArr, 1) - 1
            Local $aTemp = StringSplit($aTmp[$i], ",")
            For $j = 0 To $aTemp[0] - 1
                $aArr[$i][$j] = $aTemp[$j + 1]
            Next
        Next
        Return $aArr
    EndFunc  ;==>_CsvToArray2D
    ;========> End of _CsvToArray2D ======================
    

提交回复
热议问题