2016-04-23 57 views
0

我有一個字符串列表,像這樣的數組:VBScript來從線陣列創建樹

1=xItem 
2=xInstance 
3=xVersion 
4=xValue 
2=pInstance 
3=pVersion 
1=yItem 
2=yInstance 
2=zInstance 
3=zVersion 
4=zValue 

我有一個類定義是這樣的:

Class InfoEntry 
    Public Name 
    Public infoList 
    Private Sub Class_Initialize() 
     set infoList= CreateObject("System.Collections.ArrayList") 
    End Sub 
End Class 

我想有樹結構像這樣:

Root 
-xItem 
    -xInstance 
     -xVersion 
      -xValue 
    -pInstance 
     -pVersion 
-yItem 
    -yInstance 
    -zInstance 
     -zVersion 
      -zValue 

最初的要求只能達到兩個級別。但現在深度可能會有所不同。

我已經編寫了如下的邏輯,它只適用於兩個層次的深度。如果需要的話,我可以把我的代碼放在這裏。但它的瑕疵。我想有更好的版本。我很抱歉,我不擅長VBScript。

我不擅長VBScript,所以試圖找到VB中的邏輯。

 Function GetTree(NodeName,FileLines,counter) 
     Call WriteToLog(LOG_AL_NIINFO, "started creating Tree structure for Node " & NodeName, False) 
     Dim OEntry 
     Set OEntry = New InfoEntry 
     OEntry.Name=NodeName 
     Dim StringTree,Stack 
     set StringTree = CreateObject("System.Collections.ArrayList") 
     set Stack = CreateObject("System.Collections.ArrayList") 
     dim i,curr 
     For i=0 To (FileLines.Count-1) 
      set curr=FileLines(i) 
      call Stack.Add(curr) 
      if (i+1) < (FileLines.Count-1) then 
      if InStr(FileLines(i+1),counter & "=") = 1 then 
       call StringTree.Add(Stack) 
       set Stack= CreateObject("System.Collections.ArrayList") 
      end if 
      end if 
     Next 
     StringTree.Add(Stack) 

     If StringTree.Count > 0 Then 
      For Each collection in StringTree 
       If collection.Count > 0 Then 
        dim TopNode 
        set TopNode=collection(0) 
        call collection.Remove(TopNode) 
        dim splitKey,rootKey 
        set splitKey=Split(TopNode,"=") 
        rootKey=splitKey(1) 
        call OEntry.infoList.Add(GetTree(rootKey,collection,(counter+1))) 
       End if 
      Next 
     End If 
     Call WriteToLog(LOG_AL_NIINFO, "Finsihed creating Tree structure for Node " & NodeName, False) 

     if VMS_GCO_Structure.Exists(NodeName) = false And OEntry.infoList.Count <> 0 then 
      call VMS_GCO_Structure.Add(NodeName,OEntry) 
     end if 
     GetTree=OEntry  
end function 

我得到空引用錯誤與此。有人可以建議我在哪裏做錯了嗎?

+0

編輯你的問題,併發布你到底作爲代碼嘗試過什麼! – Hackoo

+0

done..code added..but added but but not working as per expectation .. – VyshuRam

回答

0

深度已由您的字符串指示。以下方法避免了樹和節點等,並且可以使用簡單的VBScript語句來編寫,而不需要額外的數據結構。它也適用於任意深度。 =在每一串

    • 查找位置提取的子之前
    • 將其轉換爲整數,並用4
    • 輸出相乘,許多空格
    • 提取字符串之後=
    • 輸出a - 和第二個子字符串
  • 0

    我自己找到了答案是完美的工作。我希望這會對其他人有所幫助

    Class InfoEntry 
         Public Name 
         Public isValid 
         Public infoList 
         Public childList 
         Public Sub Class_Initialize() 
          isValid = False 
          set infoList = CreateObject("System.Collections.ArrayList") 
          set childList = CreateObject("System.Collections.ArrayList") 
         End Sub 
        End Class 
    
    
    
    Function GetTree(nodeName, fileLines, counter) 
         ' Call WriteToLog(LOG_AL_NIINFO, "Indexing Tree structure for " & nodeName, False) 
         Dim oEntry 
         set oEntry = New InfoEntry 
         oEntry.Name = nodeName 
         Dim stringTree, stack 
         set stringTree = CreateObject("System.Collections.ArrayList") 
         set stack = CreateObject("System.Collections.ArrayList") 
         Dim i, j, curr 
         i = 0 
         j = FileLines.Count 
         Do While i < j 
          curr = FileLines.Item(i) 
          Call stack.Add(curr) 
          If (i + 1) < j Then 
           If InStr(FileLines(i + 1), counter & "=") = 1 Then 
            Call stringTree.Add(stack.Clone()) 
            set stack = CreateObject("System.Collections.ArrayList") 
           End If 
          End If 
          i = i + 1 
         Loop 
         Call stringTree.Add(stack.Clone()) 
         Dim topNode 
         i = 0 
         j = stringTree.Count 
         Do While i < j 
          If stringTree.Item(i).Count > 0 Then 
           topNode = stringTree.Item(i).Item(0) 
           Call stringTree.Item(i).Remove(topNode) 
           Dim splitKey, rootKey 
           splitKey = Split(topNode, "=") 
           rootKey = splitKey(1) 
           Call oEntry.infoList.Add(rootKey) 
           Call oEntry.childList.Add(GetTree(rootKey, stringTree.Item(i), (counter + 1))) 
          End If 
          i = i + 1 
         Loop 
         set GetTree = oEntry 
        End Function 
    
    +0

    這個課堂裏面的兩個arraylist是用於腳本必須做的其他puposes .. – VyshuRam