Active Server Pages: Dictionary 


The Dictionary object
 

In order to learn how Dictionary object works we will create a small script which will translate number 1 to 10 from English to Spanish.
 
 

translate.asp
<%
SET MyDictionary=CreateObject("Scripting.Dictionary")
MyDictionary.Add "one","uno"
MyDictionary.Add "two","dos"
MyDictionary.Add "three","tres"
MyDictionary.Add "four","cuatro"
MyDictionary.Add "five","cinco"
MyDictionary.Add "six","seis"
MyDictionary.Add "seven","siete"
MyDictionary.Add "eight","ocho"
MyDictionary.Add "nine","nueve"
MyDictionary.Add "ten","diez"
EnglishNumber="four"
SpanishNumber=MyDictionary.Item (EnglishNumber)
Response.Write(SpanishNumber)
%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

How the script works

We may change the values in our dictionary by using this kind of code: We may display the number of element pairs in the dictyonary by using this code: If we want to check whether a key exists in our dictionary before responding to our visitor we will use this kind of comparisoncode
 
if MyDictionary.Exists ("ten")=True  then
           Response.Write("this key is included in the dictionary")
lse
           Response.Write("Error: no such a key in the dictionary")
end if

Example: Translation of a number from English to Spanish

This example uses most of the elements explained above.

translation.asp
<html>
<title>Page under construction</title>
<body>

<% 
if request.form="" then
      Sendform()
else
      SET MyDictionary=CreateObject("Scripting.Dictionary")

MyDictionary.Add "one","uno"
MyDictionary.Add "two","dos"
MyDictionary.Add "three","tres"
MyDictionary.Add "four","cuatro"
MyDictionary.Add "five","cinco"
MyDictionary.Add "six","seis"
MyDictionary.Add "seven","siete"
MyDictionary.Add "eight","ocho"
MyDictionary.Add "nine","nueve"
MyDictionary.Add "ten","diez"
      EnglishNumber=request.form("EnglishNumber")
      Response.Write("English number: " & EnglishNumber)

      if MyDictionary.Exists (EnglishNumber)=True then
           SpanishNumber=MyDictionary.Item (EnglishNumber)
           Response.Write("<BR>Spanish number: " & SpanishNumber)
     else
           Response.Write("<BR>Spanish number: " & "No translation available")
     end if
end if
%>

<% Sub Sendform() %>
<form action=translation.asp method=post>
Write a number in English<BR>
<input type=text size=30 name=EnglishNumber><BR>
<input type=submit Value="Enter to my Secret Page">
</form>
<% End Sub %>

</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

 

Example: Password protected information

In this example keys and items are used as usernames and passwords. It is very similar to the one above.
 
 

secretpage.asp
<% 
if request.form="" then
      Sendform()
else
      SET MyDictionary=CreateObject("Scripting.Dictionary")
      MyDictionary.Add "John","123"
      MyDictionary.Add "Peter","456"
      MyDictionary.Add "Anna","789"
      Username=request.form("Username")
      Password=request.form("password")

      if MyDictionary.Exists (Username)=True AND Password=MyDictionary.Item (Username) then
            SecretInfo() 
     else
           Response.Write("Error: incorrect userame or password")
     end if
end if
%>

<% Sub Sendform() %>
<form action=secretpage.asp method=post>
Username: <input type=text size=30 name=Username><BR>
Password: <input type=password size=30 name=Password><BR>
<input type=submit Value="Submit">
</form>
<% End Sub %>
 

<% Sub SecretInfo() %>
<html>
<head>
<title>My Secret Page</title>
</head>
<body bgcolor=FFFFFF>
<center>
<h1>This is my secret info</h1>
Hello !<BR>
Do you want to be my friend?
</center>
</body>
</html>
<% End Sub %>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

 

Tutorial home page