I googled around for some info. about dictionary objects used to simulate Perl's Hash of Hashes and found nothing. So I hacked a while and here is a subroutine to demonstrate what I learned.
Sub TestHoH()
'Example equivalent of a hash of hashes
Dim Dict1 As Object
Dim Key1, Key2
Dim Str As String
Set Dict1 = CreateObject("Scripting.Dictionary")
Dict1.Add "A", CreateObject("Scripting.Dictionary") 'Set item value to be another dictionary object (hash)
Dict1.Item("A").Item("A") = "A1" 'Demonstrate autovivification
Dict1.Item("A").Add "B", "B1"
Dict1.Add "B", CreateObject("Scripting.Dictionary")
Dict1.Item("B").Add "A", "A2" 'Instead of autovivification
Dict1.Item("B").Add "B", "B2"
For Each Key1 In Dict1
For Each Key2 In Dict1.Item(Key1)
Str = Str & Key1 & Key2 & "=" & Dict1.Item(Key1).Item(Key2) & Chr(13)
Next Key2
Next Key1
MsgBox Str
'And, a little more Perlish
'Add another hash to the parent hash
Set Dict1("C") = CreateObject("Scripting.Dictionary")
Dict1("C")("A") = "C1" 'This looks a little more like Perl
Dict1("C")("B") = "C2"
Str = ""
For Each Key1 In Dict1
For Each Key2 In Dict1.Item(Key1)
Str = Str & Key1 & Key2 & "=" & Dict1(Key1)(Key2) & Chr(13)
Next Key2
Next Key1
MsgBox Str
'Test the Exists method, using this Perlish style
If Dict1("C").Exists("B") Then
MsgBox Dict1("C")("B")
Else
MsgBox "Dict(C)(B) does not exist"
End If
If Dict1("C").Exists("C") Then
MsgBox Dict1("C")("C")
Else
MsgBox "Dict(C)(C) does not exist"
End If
End Sub
I love programming using VBA Excel and Perl. Comments are, of course, welcome.
Bookmarks