Its tempting to advise you to use JScript on the server side instead of VBScript. Not only does it do this sort of thing more naturally, you are likely to be familiar with the language. The downside is that the vast majority of "How-To" on the Web related to ASP is written in VBScript.
The associative array in VBScript is called the Dictionary
is available from the Scripting
library. However to create a heirarchical structure you will probably need a little more help. I would create a class around the Dictionary
so that I can hold more that just a Name
property and to make heirarchical manipulation easier.
Here is a sample class:-
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
Now you can create your tree:-
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
Now access this heirarchy as:-
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