2012-05-16 14 views
5

Mam problem z zapełnieniem tablicy przy użyciu polecenia split.Array i Split polecenia, aby utworzyć dwuwymiarową tablicę

Ciąg Mam aktualnie jest poniżej

MyString = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" & vbNewLine & _ 
"Row2 Column1[~]Row2 Column2[~]Row2 Column3" & vbNewLine & _ 
"Row3 Column1[~]Row3 Column2[~]Row3 Column3" & vbNewLine & _ 
"Row4 Column1[~]Row4 Column2[~]Row4 Column3" 

Mam tablicę, że chcę być wielowymiarowe i chciałby każdy wiersz # kolumny #, aby być w odpowiedniej części tablicy na podstawie jego numer.

Na przykład

MyArray(1,1) = "Row1 Column1" 
MyArray(2,1) = "Row2 Column1" 
MyArray(3,1) = "Row3 Column1" 
MyArray(4,1) = "Row4 Column1" 

MyArray(1,2) = "Row1 Column2" 
MyArray(2,2) = "Row2 Column2" 
MyArray(3,2) = "Row3 Column2" 
MyArray(4,2) = "Row4 Column2" 

MyArray(1,3) = "Row1 Column3" 
MyArray(2,3) = "Row2 Column3" 
MyArray(3,3) = "Row3 Column3" 
MyArray(4,3) = "Row4 Column3" 

Teraz rozumiem, jak wypełnić jeden wymiar tablicy przy użyciu polecenia podziału

MyArray = Split(MyString, vbNewLine) 

Oznaczałoby to, że

MyArray(1) = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" 
MyArray(2) = "Row2 Column1[~]Row2 Column2[~]Row2 Column3" 
MyArray(3) = "Row3 Column1[~]Row3 Column2[~]Row3 Column3" 
MyArray(4) = "Row4 Column1[~]Row4 Column2[~]Row4 Column3" 

ale nie wiedzieć, jak użyć polecenia podziału, aby zapełnić drugi wymiar.

Czy to możliwe i czy tak jest?
Jeśli nie jest to możliwe, czy ktoś może zasugerować, jak to zapełnić?

Odpowiedz

5

Nie można użyć metody Split() do niczego innego niż ciąg lub wariant zawierający ciąg. Jeśli chcesz wygenerować dwuwymiarową tablicę ciągów, będziesz musiał iterować przez tablicę zwróconą przez Split() i uruchomić Split() na każdym łańcuchu. Poniższa funkcja powinna zrobić to, co chcesz:

Private Function SplitTo2DArray(ByRef the_sValue As String, ByRef the_sRowSep As String, ByRef the_sColSep As String) As String() 

    Dim vasValue     As Variant 
    Dim nUBoundValue    As Long 
    Dim avasCells()     As Variant 
    Dim nRowIndex     As Long 
    Dim nMaxUBoundCells    As Long 
    Dim nUBoundCells    As Long 
    Dim asCells()     As String 
    Dim nColumnIndex    As Long 

    ' Split up the table value by rows, get the number of rows, and dim a new array of Variants. 
    vasValue = Split(the_sValue, the_sRowSep) 
    nUBoundValue = UBound(vasValue) 
    ReDim avasCells(0 To nUBoundValue) 

    ' Iterate through each row, and split it into columns. Find the maximum number of columns. 
    nMaxUBoundCells = 0 
    For nRowIndex = 0 To nUBoundValue 
     avasCells(nRowIndex) = Split(vasValue(nRowIndex), the_sColSep) 
     nUBoundCells = UBound(avasCells(nRowIndex)) 
     If nUBoundCells > nMaxUBoundCells Then 
      nMaxUBoundCells = nUBoundCells 
     End If 
    Next nRowIndex 

    ' Create a 2D string array to contain the data in <avasCells>. 
    ReDim asCells(0 To nUBoundValue, 0 To nMaxUBoundCells) 

    ' Copy all the data from avasCells() to asCells(). 
    For nRowIndex = 0 To nUBoundValue 
     For nColumnIndex = 0 To UBound(avasCells(nRowIndex)) 
      asCells(nRowIndex, nColumnIndex) = avasCells(nRowIndex)(nColumnIndex) 
     Next nColumnIndex 
    Next nRowIndex 

    SplitTo2DArray = asCells() 

End Function 

Przykład:

Dim asCells() As String 

asCells() = SplitTo2DArray(MyString, vbNewline, "~") 
+0

Myślałem, że tak być może, i potrzebowałbym innej metody. Dziękuję bardzo, twoja funkcja działa idealnie. –

+0

Jeśli chcesz podekscytować i nie masz nic przeciwko temu, by mieć indeks kolumny i wiersza na odwrót, wypróbuj odpowiedź wqw! –

4

Tutaj jest szybszy siekać, że tylko zamienia 1D i 2D tablice bez pętli:

Option Explicit 

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 

Private Sub Form_Load() 
    Dim MyString  As String 
    Dim MyFlatArray  As Variant 
    Dim MyArray   As Variant 

    '--- split source string by column&row separator 
    MyString = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" & vbNewLine & _ 
     "Row2 Column1[~]Row2 Column2[~]Row2 Column3" & vbNewLine & _ 
     "Row3 Column1[~]Row3 Column2[~]Row3 Column3" & vbNewLine & _ 
     "Row4 Column1[~]Row4 Column2[~]Row4 Column3" 
    MyFlatArray = Split(Replace(MyString, "[~]", vbCrLf), vbCrLf) 
    '--- convert to 2D array 
    ReDim MyArray(1 To 3, 1 To 4) As String 
    pvSwapArrays MyArray, MyFlatArray 
    '--- access row 2 
    Debug.Print MyArray(1, 2) 
    Debug.Print MyArray(2, 2) 
    Debug.Print MyArray(3, 2) 
End Sub 

Private Sub pvSwapArrays(vDst As Variant, vSrc As Variant) 
    Dim nDstType  As Integer 
    Dim nSrcType  As Integer 
    Dim pSrcArray  As Long 
    Dim pDstArray  As Long 
    Dim lTemp   As Long 

    '--- sanity check types (VARIANT.vt) 
    Call CopyMemory(nDstType, vDst, 2) 
    Call CopyMemory(nSrcType, vSrc, 2) 
    Debug.Assert (nSrcType And &H2000) <> 0 '--- check if VT_ARRAY 
    Debug.Assert nDstType = nSrcType '--- check if dest type matches src (&H2008 = VT_ARRAY | VT_BSTR) 
    '--- get VARIANT.parray 
    Call CopyMemory(pSrcArray, ByVal VarPtr(vSrc) + 8, 4) 
    Call CopyMemory(pDstArray, ByVal VarPtr(vDst) + 8, 4) 
    '--- swap SAFEARRAY.pvData 
    Call CopyMemory(lTemp, ByVal pSrcArray + 12, 4) 
    Call CopyMemory(ByVal pSrcArray + 12, ByVal pDstArray + 12, 4) 
    Call CopyMemory(ByVal pDstArray + 12, lTemp, 4) 
End Sub 
+0

Ha! Fajny hack! Gdybym jednak chciał to wykorzystać, chciałbym uzyskać dłuższe wyjaśnienie tego, co robiłeś. Robiąc to po swojemu, jesteś zmuszony do numerowania indeksów tablicy (kolumna, wiersz). –

+0

@MarkBertenshaw: wewnętrzna reprezentacja tablic wymusza niższe obwiednie jako pierwsze podczas indeksowania. Jeśli użyjesz 'For Each' w tablicy 2D, to elementy kolejności są zapętlone. Obydwie wielkości tablic muszą być takie same lub zaryzykować AV podczas odrywania. Wszystkie te ograniczenia są kompensowane przez brak jakichkolwiek pętli i brak kopiowania ciągów. – wqw

+0

Wiedziałem, jak działają SAFEARRAY, ale nigdy nie myślałem o używaniu For..Each..Next z tablicą non-1D! Nawet w nieobsługiwanym języku wciąż jest więcej rzeczy do nauczenia się. –

0

Oto inne podejście oparte na zdolności typu Wariant do zawarcia tablicy. Zamiast tablicy 2D mamy "tablicę tablic".

Option Explicit 

Private Function SplitSplit(ByRef Delimited As String) As Variant 
    Dim Rows() As String 
    Dim AryOfArys As Variant 
    Dim I As Long 

    Rows = Split(Delimited, vbNewLine) 
    ReDim AryOfArys(UBound(Rows)) 
    For I = 0 To UBound(Rows) 
     AryOfArys(I) = Split(Rows(I), "[~]") 
    Next 
    SplitSplit = AryOfArys 
End Function 

Private Sub Form_Load() 
    Dim MyString As String 
    Dim MyAry As Variant 

    MyString = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" & vbNewLine _ 
      & "Row2 Column1[~]Row2 Column2[~]Row2 Column3" & vbNewLine _ 
      & "Row3 Column1[~]Row3 Column2[~]Row3 Column3" & vbNewLine _ 
      & "Row4 Column1[~]Row4 Column2[~]Row4 Column3" 

    MyAry = SplitSplit(MyString) 

    AutoRedraw = True 
    DumpAry MyAry 
End Sub 

Private Sub DumpAry(ByRef AryOfArys As Variant) 
    Dim Row As Long, Col As Long 

    For Row = 0 To UBound(AryOfArys) 
     For Col = 0 To UBound(AryOfArys(Row)) 
      Print AryOfArys(Row)(Col), 
     Next 
     Print 
    Next 
End Sub 

Bonusem jest tutaj "poszarpane tablice", gdzie każdy wiersz może mieć inną liczbę kolumn.

Powiązane problemy