Results 1 to 1 of 1

Example equivalent Hash of Hashes

Threaded View

jdawson Example equivalent Hash of... 07-26-2008, 09:20 PM
  1. #1
    Registered User
    Join Date
    03-06-2006
    Location
    South Central Pennsylvania
    MS-Off Ver
    MS Office Pro Plus 2016, on Window 10
    Posts
    27

    Example equivalent Hash of Hashes

    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.
    Last edited by jdawson; 07-26-2008 at 10:57 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1