2012-05-16 14 views
5

मुझे स्प्लिट कमांड का उपयोग करके एक सरणी पॉप्युलेट करने में कुछ परेशानी हो रही है।एरे और स्प्लिट कमांड 2 आयामी सरणी बनाने के लिए

स्ट्रिंग मैं वर्तमान में है

नीचे
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" 

मैं एक सरणी है कि मैं बहुआयामी होना चाहते है और प्रत्येक पंक्ति # कॉलम # चाहते हैं के आधार पर सरणी के सही भाग में होने की नंबर।

उदाहरण के लिए

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" 

अब मैं समझता हूँ विभाजन आदेश

MyArray = Split(MyString, vbNewLine) 

का उपयोग कर एक भी आयाम सरणी को भरने के लिए कैसे इसका मतलब यह होगा कि

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" 

लेकिन मुझे नहीं पता दूसरे आयाम को पॉप्युलेट करने के लिए स्प्लिट कमांड का उपयोग कैसे करें।

क्या यह संभव है और यदि ऐसा है तो?
यदि यह संभव नहीं है, तो क्या कोई सुझाव दे सकता है कि वास्तव में इसे कैसे पॉप्युलेट करना है?

उत्तर

5

आप स्ट्रिंग के अलावा किसी भी चीज़ पर स्प्लिट() का उपयोग नहीं कर सकते हैं, या एक स्ट्रिंग युक्त वेरिएंट का उपयोग नहीं कर सकते हैं। यदि आप दो आयामी स्ट्रिंग सरणी उत्पन्न करना चाहते हैं, तो आप स्प्लिट() द्वारा लौटाए गए सरणी के माध्यम से पुनरावृत्ति करेंगे, और प्रत्येक स्ट्रिंग पर स्प्लिट() चलाएं। निम्नलिखित समारोह आप क्या चाहते हैं करना चाहिए:

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 

उदाहरण:

Dim asCells() As String 

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

मैंने सोचा कि यह मामला हो सकता है, और मुझे एक अलग विधि की आवश्यकता होगी। बहुत बहुत धन्यवाद, आपका फ़ंक्शन पूरी तरह से काम करता है। –

+0

यदि आप उत्तेजना चाहते हैं, और कॉलम और पंक्ति अनुक्रमणिका को दूसरी तरफ रखने में कोई फर्क नहीं पड़ता है, तो wqw का जवाब आज़माएं! –

4

यहाँ एक तेजी से हैक किया जाता है कि बस कोई छोरों के साथ -1 डी और 2 डी सरणियों स्वैप:

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

हा! अच्छा हैक! अगर मैं इसका इस्तेमाल करना चाहता था, हालांकि, मैं आप जो कर रहा था उसके बारे में एक लंबा स्पष्टीकरण चाहता हूं। इसके अलावा, इसे अपना रास्ता बनाते हुए, आपको सरणी सूचकांक (कॉलम, पंक्ति) की संख्या के लिए मजबूर होना पड़ता है। –

+0

@ मार्कबर्टनशॉ: इंडेक्सिंग के दौरान आंतरिक सरणी प्रतिनिधित्व बल कम आने के लिए मजबूर करता है। यदि आप 2 डी सरणी पर 'प्रत्येक के लिए' का उपयोग करते हैं, तो ऑर्डर तत्व लूप किए जाते हैं। दोनों सरणी कुल आकारों को भी मिलना चाहिए या आंसू पर एवी जोखिम होना चाहिए। इन सभी बाधाओं को किसी भी लूप की कमी और कोई स्ट्रिंग प्रतिलिपि नहीं है। – wqw

+0

मुझे पता था कि सुरक्षित कैसे काम करते हैं, लेकिन मैंने कभी भी गैर-1 डी सरणी के साथ ..Each..Next का उपयोग करने के बारे में सोचा नहीं है! एक असमर्थित भाषा में भी, सीखने के लिए और भी चीजें हैं। –

0

यहाँ है वेरिएंट प्रकार की सरणी रखने की क्षमता के आधार पर एक और दृष्टिकोण। 2 डी सरणी के बजाय हमारे पास "सरणी की सरणी" है।

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 

यहां बोनस "रैगर्ड एरे" है जहां प्रत्येक पंक्ति में कॉलम की एक अलग संख्या हो सकती है।

संबंधित मुद्दे