Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

+91-943-185-6038 me@shashidharkumar.com

ASP - Dictionary

ASP – 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

  • Fist we have define a Dictionary named “Mydictionary” (line 2)
  • We have add to the dictionary the data corresponding to the different number in English and Spanish (lines 4 to 13).
    When adding pairs of English and Spanish numbers to the Dictionary object, the number writen in English is a Key, and the number writen in Spanish a Item.
  • In line 15 we have defined a variable named EnglishNumber and we have provided a value for this variable (in red).
  • In line 16 we have defined a new variable (SpanishNumber) and we have get its value from the dictionary by indicating we want to get the Item corresponding to a specific Key (EnglishNumber).
  • In line 17 the translated number is send to our visitor. The response will be “cuatro”.

We may change the values in our dictionary by using this kind of code:

  • MyDictionary.Key (“one”)=”1″
    In our original script the key “one” will be substitute by a new key value (“1”). The item “uno” will not be changed.
  • MyDictionary.Item (“two”)=”2″
    In our original script the item corresponding to key “two” will be substitute by a new item value (“2”). The key “two” will not be changed.

We may display the number of element pairs in the dictyonary by using this code:

  • MyDictionary.Count

If we want to check whether a key exists in our dictionary before responding to our visitor we will use this kind of comparison code

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.

  • If there is no information posted to the script (line 6), the script will send to the visitor the form in the Sendform() subrouting (lines 34-40).
  • When a request to translate a number is get the script will check whether the number corresponds to a key in the dictionary (line 25). If the response is afirmative the corresponding item is send to the visitor. In case the key does not exists, a “No translation available” response is send to the visitor (line 29).
translation.asp
<html>
<title>Page under construction</title>
<body><%¼br> 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.
<br>

secretpage.asp
<%¼br> 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()¼br> 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
See also  ASP - Array