views:

190

answers:

6

PHP provides associative arrays but there is no such thing in classical asp. I am looking for a class or function that allows me to create and traverse a hierarchy similar to this:

United States
  Washington
    Electric City
      Banks Lake 
    Lake Chelan
      Wapato Point 
    Gig Harbour
Mexico
  Nayarit
    Aticama
Asia
  India
    Mumbai
  Pakistan
    Sindh
      Karachi

As you can notice, the hierarchy has uneven depth -- it could be 3, 4 or 5 levels deep. The database structure is even more complicated as for some records I have to start displaying data from region level while for others I should start from country. An array of arrays would really help but in ASP my hands are tied.

A: 

use Server.CreateObject("Scripting.Dictionary")

http://msdn.microsoft.com/en-us/library/x4k5wbx4%28VS.85%29.aspx

Stijn Sanders
I can't see how to make a hierarchy of a hash table.
Jonas Elfström
A: 

You might want to take a look at the the materialized path model on the sql side. This really simplefies working with relative 'static' trees.

Joost Moesker
+1  A: 

You could put the hierarchy in a XMLDOM object and traverse over it that way.

Jonas Elfström
This looks promising too.
Salman A
+4  A: 

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
AnthonyWJones
Yes i personally admire the flexibility that javascript provides -- but to use javascript on server side I'll have to rewrite the entire app.
Salman A
Hey your code sample looks promising -- supports recursion, unlimited depth and wise use of IsObject, Set and Nothing statements. I'll check.
Salman A
The XMLDOM idea was good too but your code is more adaptable for my needs.
Salman A
A: 

I would create a class to contain these, something like this:

Class Area
  Public Name
  Private _children

  Private Sub Class_Initialize()
    _children = Array()
  End Sub

  Public Property Get Children()
    Set Children = _children
  End Property

  Public Sub AddChild(newChild)
    ReDim Preserve _children(UBound(_children)+1)
    Set _children(UBound(_children)) = newChild
  End Sub
End Class
svinto
A: 

I tried to implement in .vbs not used in ASP for web based approach... But it is showing error... I have used the below modified portion...

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 'wscript.echo key 'wscript.echo root.Child(key).Name & vbCrlF If IsObject(root.Child(key)) Then if Not IsNull(root.Child(key)) then wscript.echo IsObject(root.Child(key))&"...." WriteChildrenToResponse root.Child(key) else wscript.echo "Object is Null..." end if Else wscript.echo "Not an Object..." End If next

End Sub

It is throwing following error:

runtime error: Object required: 'Node'

Sudipto