Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a dictionary object have multiple Items under same key?

Tags:

vbscript

I a looking for an workaround related to dictionary objects items

Dim a, d 'Create some variables

 Set d = CreateObject("Scripting.Dictionary")

 d.Add "a", "Athens" 'is possible I know 

 d.Add "a", "Athens","India","Paris" ' Is this Possible  under same Key?

Description snapshot:(UPDATED)

  Manger ID            EMPID      EMPID     EMPID     EMPID ......

     11                12          10
     15                10 
     20                22          45        46
     40

How the above table can then be implemented using dictionary object? Give me some idea.

Thanks,

like image 779
arun_roy Avatar asked Dec 19 '12 07:12

arun_roy


2 Answers

EDIT: Makes variable name more OPfriendly

EDIT: Include a Scripting.Dictionary Reference for OP's reading

Sub t()
    Dim d
    Dim a
    a = Array("Athens", "India", "Paris")
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "a", a
    MsgBox Join(d.Item("a"), ",")
End Sub

EDIT: to read values as in OP's question into dictionary and retrieve the values

Sub t()
    Dim d As Object
    Dim values
    Dim height As Long
    Dim item
    Dim width As Long
    Dim i As Long, j As Long
    Dim MANGERID
    Dim EMPIDs
    Dim EMPID
    With ActiveSheet
        height = .Cells(.Rows.Count, 1).End(xlUp).Row
        If height < 2 Then
            Exit Sub
        End If
        Set d = CreateObject("Scripting.Dictionary")
        For i = 2 To height
            width = .Cells(i, .Columns.Count).End(xlToLeft).Column
            if width > 1 then  'make sure have EMPID
                ReDim values(1 To width - 1)
                Key = .Cells(i, 1).Value
                For j = 2 To width
                    values(j - 1) = .Cells(i, j).Value
                Next j
                d.Add Key, values
            end if
        Next i

        'displaying back the items in the dictionary
        For Each MANGERID In d.keys
            'value array
            EMPIDs = d.item(MANGERID)
            If TypeName(EMPIDs) = "Variant()" Then
                For Each EMPID In EMPIDs
                    Debug.Print MANGERID & ":" & EMPID
                Next EMPID
            End If
        Next MANGERID

        Set d = Nothing
    End With
End Sub
like image 165
Larry Avatar answered Sep 22 '22 14:09

Larry


No. By definition, dictionary data types use keys that must be unique. I know it's like this on most implementations, but the closest I can come to for an authoritative reference for VBscript's Scripting.Dictionary is this TechNet blurb:

A key is a unique entry: no two keys within a single Dictionary object can be the same.

like image 41
Jonathon Reinhart Avatar answered Sep 23 '22 14:09

Jonathon Reinhart