VBScript の代わりにサーバー側で JScript を使用することをお勧めします。この種のことをより自然に行うだけでなく、言語に慣れる可能性が高くなります。欠点は、ASP に関連する Web 上の「ハウツー」の大部分が VBScript で記述されていることです。
VBScript の連想配列は、ライブラリDictionary
から入手できると呼ばれます。Scripting
ただし、階層構造を作成するには、おそらくもう少し助けが必要です。の周りにクラスを作成してDictionary
、単なるName
プロパティ以上のものを保持できるようにし、階層的な操作を容易にします。
サンプルクラスは次のとおりです。
Class Node
Private myName
Private myChildren
Private Sub Class_Initialize()
Set myChildren = CreateObject("Scripting.Dictionary")
End Sub
Public Property Get Name()
Name = myName
End Property
Public Property Let Name(value)
myName = Value
End Property
Public Function AddChild(value)
If Not IsObject(value) Then
Set AddChild = new Node
AddChild.Name = value
Else
Set AddChild = value
End If
myChildren.Add AddChild.Name, AddChild
End Function
Public Default Property Get Child(name)
Set Child = ObjectOrNothing(myChildren.Item(name))
End Property
Public Property Get Children()
Set Children = myChildren
End Property
Private Function ObjectOrNothing(value)
If IsObject(value) Then
Set ObjectOrNothing = value
Else
Set ObjectOrNothing = Nothing
End If
End Function
End Class
これでツリーを作成できます:-
Dim root : Set root = new Node
With root.AddChild("United States")
With .AddChild("Washington")
With .AddChild("Electric City")
.AddChild "Banks Lake"
End With
With .AddChild("Lake Chelan")
.AddChild "Wapato Point"
End With
.AddChild "Gig Harbour"
End With
End With
この階層に次のようにアクセスします:-
Sub WriteChildrenToResponse(node)
For Each key In node.Children
Response.Write "<div class=""node"">" & vbCrLf
Response.Write "<div>" & root.Child(key).Name "</div>" & vbCrlF
Response.Write "<div class=""children"">" & vbCrLf
WriteChildrenToResponse root.Child(key)
Response.Write "</div></div>"
Next
End Sub
''# Dump content of root heirarchy to the response
WriteChildrenToResponse root