<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; VBScript: двухуровневый Dictionary]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=3907&amp;type=atom" />
	<updated>2009-11-22T09:46:19Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=3907</id>
		<entry>
			<title type="html"><![CDATA[VBScript: двухуровневый Dictionary]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=30958#p30958" />
			<content type="html"><![CDATA[<p>Часто возможностей стандартного объекта Dictionary не хватает из-за того, что он одноуровневый. Поскольку в качестве item данного объекта может быть любой иной объект, то напрашивается там хранить другой словарь, тем самым реализуя двух и более уровневые словари. Привожу пример кода для 2-х уровневого варианта:<br /></p><div class="codebox"><pre><code>Set objRootDict = CreateObject(&quot;Scripting.Dictionary&quot;)

arrTest = Array(10,20,30) &#039; в качестве полезной нагрузке будем хранить, например, массив
Add_Element &quot;A&quot;, &quot;1&quot;, arrTest &#039; добавляем элемент в коллекцию
arrTest = Array(40,50,60)
Add_Element &quot;A&quot;, &quot;2&quot;, arrTest
arrTest = Array(70,80,90)
Add_Element &quot;B&quot;, &quot;1&quot;, arrTest
WScript.Echo Get_Element(&quot;B&quot;, &quot;1&quot;)(2) &#039; получаем элемент из коллекции

&#039; Перебор всей коллекции и вывод на экран
For Each dbgMainKey In objRootDict.Keys
    Set objTempDict = objRootDict.Item(dbgMainKey)
    For Each dbgKey In objTempDict.Keys
        arrValueList = objTempDict.Item(dbgKey)
        s = dbgMainKey &amp; vbTab &amp; dbgKey
        For i = 0 To UBound(arrValueList)
            s = s &amp; vbTab &amp; CStr(arrValueList(i))
        Next
        WScript.Echo s
    Next
Next

Sub Add_Element(parLevel_1, parVevel_2, parValue)
    If Not objRootDict.Exists(parLevel_1) Then
        Set objTempDict = CreateObject(&quot;Scripting.Dictionary&quot;)
        objRootDict.Add parLevel_1, objTempDict
    Else
        Set objTempDict = objRootDict.Item(parLevel_1)
    End If
    objTempDict.Add parVevel_2, parValue
End Sub
    
Function Get_Element(parLevel_1, parVevel_2)
    If objRootDict.Exists(parLevel_1) Then
        Set objTempDict = objRootDict.Item(parLevel_1)
        If objTempDict.Exists(parVevel_2) Then 
            Get_Element = objTempDict.Item(parVevel_2)
        Else
            Get_Element = Null
        End If
    else
        Get_Element = Null
    End If
End Function</code></pre></div>]]></content>
			<author>
				<name><![CDATA[dsb]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24622</uri>
			</author>
			<updated>2009-11-22T09:46:19Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=30958#p30958</id>
		</entry>
</feed>
