ASP

ASP Variables Strings Form Arrays 2D Array Loop IF Cookies Dates Dictionaries SQL Numbers Files JSON Database Regular Expression ExecuteGlobal Recordset ServerVariables Bugs ADO AJAX XML Chilkat
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

ASP : Dictionaries

2017-01-01

Dictionaries are case-sensitive.

Basics

//CREATE
Set dict = Server.CreateObject("Scripting.Dictionary")
dict.Add "moduleID", 11
dict.Add "recordField", "InterestID"
dict.Add 1, "Santa Ana"  //Key can be a number

//READ
Response.Write(dict.Item("recordField"))
Response.Write(dict("recordField"))  //abbreviated syntax

//UPDATE
dict("recordField") = "Hello"

//DELETE
dict.Remove("recordField")
dict.RemoveAll  //deletes all items in dictionary

More

//LOOP THROUGH DICTIONARY
For Each key In dict
    response.write key & " - " & dict(key) & "<br>"
Next

//IF EXISTS
if dict.Exists("recordField") then response.write "It exists"

Print Dictionary For Debugging

function printDictionary (dict)
    'For Debugging : Prints dictionary key/value
    if TypeName(dict) <> "Dictionary" then exit function
    For Each key In dict
        if InStr("String Integer Double IStringList Empty",typeName(dict(key)))  then
            ret = ret & "<b style='font-family:arial;'>" & key & ":</b> <span style='font-family:courier;'>" & dict(key) & "</span><br>"
        else
            ret = ret & "<b style='font-family:arial;'>" & key & ":</b>  <span style='font-family:courier;'><i style='color:red;'>OBJECT:</i> " & typeName(dict(key)) & "</span><br>"
        end if
    Next
    printDictionary = "<b style='font-family:arial;color:blue;'>==== DICTIONARY ITEMS ====</b><br>" & ret & "<b style='font-family:arial;color:blue;'>==========================</b>"
end function

Put All POST/GET Requests Into A Dictionary

function requestsToDict()
    'Puts all form data in a dictionary 
    Set dict = Server.CreateObject("Scripting.Dictionary")
    For Each temp In Request.Form
        dict.Add temp,Request.Form(temp)
    Next
    For Each temp In Request.QueryString
        dict.Add temp,Request.QueryString(temp)
    Next
    set requestsToDict = dict
    set dict = nothing
end function

Combine 2 Dictionaries Into 1

function combineDict(dict1,dict2)
    'Combines 2 dictionaies into 1
    Set dict3 = Server.CreateObject("Scripting.Dictionary")
    For Each key In dict1
        dict3.Add key, dict1(key)
    Next    
    For Each key In dict2
        dict3.Add key, dict2(key)
    Next
    set combineDict = dict3
    set dict3 = nothing
end function

Convert a dictionary to JSON string.

Set successDict = Server.CreateObject("Scripting.Dictionary")
successDict.Add "message","Passed security."
successDict.Add "status","2"
response.write dictToJSON(successDict)
set successDict=nothing
response.end

function dictToJSON(dict) 
    allKeys = dict.Keys   'Get all the keys into an array
    allItems = dict.Items 'Get all the items into an array
    ret = ""
    For i = 0 To dict.Count - 1 'Iterate through the array
        ret = ret & """" &allKeys(i)& """:""" &allItems(i)& ""","
    Next
    ret = left(ret,len(ret)-1)
    dictToJSON = "{" &ret& "}"
end function

// OUTPUT
// {"status":"4","message":"Invalid Key."}

References:

http://webcheatsheet.com/asp/dictionary_object.php

https://www.itprotoday.com/management-mobility/understanding-vbscript-dictionary-object-alternative-arrays