将字符串拆分为多维(2D)数组

时间:2018-01-05 19:02:52

标签: arrays multidimensional-array split autoit

我尝试将字符串拆分为多维数组,但它不能按我想要的方式工作。我只是将一个字符串拆分为一些数组,其中一个二维数组应该表示一个表。我可以使用_ArrayDisplay()看到数组,但我不能使用数组元素本身。线:

For $j = 1 To $aTable1Row[$i][0]

报告"下标数量不正确"。但如果我这样做:

MsgBox(1, "TEST", UBound($aTable1Row[$i]))

它显示此数组有8个元素。所以他们在那里但我无论如何都无法访问它们。完整源代码:

#include <Array.au3>

$string = "az#1:y#2:x#3:w#4:v#5:u#6:t#7-bz#1:y#2:x#3:w#4:v#5:u#6:t#7-cz#1:y#2:x#3:w#4:v#5:u#6:t#7"

$aTable1 = StringSplit($string, '-',1)
_ArrayDisplay($aTable1)
;3
;az#1:y#2:x#3:w#4:v#5:u#6:t#7
;bz#1:y#2:x#3:w#4:v#5:u#6:t#7
;cz#1:y#2:x#3:w#4:v#5:u#6:t#7

Local $aTable1Row[$aTable1[0]+1]
$aTable1Row[0] = $aTable1[0]    

For $i = 1 To $aTable1Row[0]
    $aTable1Row[$i] = StringSplit($aTable1[$i], ':',1)
    _ArrayDisplay($aTable1Row[$i])
    ;7
    ;az#1
    ;y#2
    ;x#3
    ;w#4
    ;v#5
    ;u#6
    ;t#7

    ;do stuff
    For $j = 1 To $aTable1Row[$i][0]
        $aTable1Row[$i][$j] = StringTrimLeft(($aTable1Row[$i])[$j], StringInStr(($aTable1Row[$i])[$j], '#'))
    Next
Next

2 个答案:

答案 0 :(得分:0)

  

...这个数组有8个元素。

For -loop将1维数组视为2维数组,因此Array variable has incorrect number of subscripts or subscript dimension range exceeded. - 错误。

首先

Declare a 2 dimensional array。示例(无错误检查):

#include <StringConstants.au3>
#include <Array.au3>

Global Const $g_sString   = 'az#1:y#2:x#3:w#4:v#5:u#6:t#7-bz#1:y#2:x#3:w#4:v#5:u#6:t#7-cz#1:y#2:x#3:w#4:v#5:u#6:t#7'
Global Const $g_sDelimRow = '-'
Global Const $g_sDelimCol = ':'
Global Const $g_aArray2D  = StringSplitToArray2D($g_sString, $g_sDelimRow, $g_sDelimCol)

_ArrayDisplay($g_aArray2D, @ScriptName)

Func StringSplitToArray2D(Const $sString, Const $sDelimiterRow, Const $sDelimiterCol)
    Local $aArray1DRows = StringSplit($sString,         $sDelimiterRow, $STR_ENTIRESPLIT)
    Local $aArray1DCols = StringSplit($aArray1DRows[1], $sDelimiterCol, $STR_ENTIRESPLIT)
    Local $aArray2D[ $aArray1DRows[0] + 1 ][ $aArray1DCols[0] + 1 ]

    For $i1 = 1 To $aArray1DRows[0]

        $aArray1DCols = StringSplit($aArray1DRows[$i1], $sDelimiterCol, $STR_ENTIRESPLIT)

        For $i2 = 1 To $aArray1DCols[0]

            $aArray2D[$i1][$i2] = $aArray1DCols[$i2]

        Next

    Next

    Return $aArray2D
EndFunc

如果存储为text / CSV文件,请考虑_FileReadToArray()Related

答案 1 :(得分:0)

#include <StringConstants.au3>
#include <Array.au3>

Global Const $g_sString   = 'az#1:y#2:x#3:w#4:v#5:u#6:t#7-bz#1:y#2:x#3:w#4:v#5:u#6:t#7-cz#1:y#2:x#3:w#4:v#5:u#6:t#7'
Global Const $g_sDelimRow = '-'
Global Const $g_sDelimCol = ':'
Global Const $g_aArray2D  = StringSplit2D($g_sString, $g_sDelimCol, $g_sDelimRow)

_ArrayDisplay($g_aArray2D, @ScriptName)

Func StringSplit2D($sMatches = "Hola-2-5-50-50-100-100|Hola-6-200-200-100-100", Const $sDelim_Item = "-", Const $sDelim_Row = "|")
    Local $iValDim_1, $iValDim_2 = 0, $iColCount
    Local $aSplit_1 = StringSplit($sMatches, $sDelim_Row, $STR_NOCOUNT + $STR_ENTIRESPLIT)
    $iValDim_1 = UBound($aSplit_1, $UBOUND_ROWS)
    Local $aTmp[$iValDim_1][0], $aSplit_2
    For $i = 0 To $iValDim_1 - 1
        $aSplit_2 = StringSplit($aSplit_1[$i], $sDelim_Item, $STR_NOCOUNT + $STR_ENTIRESPLIT)
        $iColCount = UBound($aSplit_2)
        If $iColCount > $iValDim_2 Then
            $iValDim_2 = $iColCount
            ReDim $aTmp[$iValDim_1][$iValDim_2]
        EndIf
        For $j = 0 To $iColCount - 1
            $aTmp[$i][$j] = $aSplit_2[$j]
        Next
    Next
    Return $aTmp
EndFunc   ;==>StringSplit2D

这是一个非常稳定的功能,它被用于大量项目,经过数百万次测试。

Result