E-learning Center - Only free tutorials and information

Content:

PHP book

Sybase

Free scripts

Tutorials

ASP


Ads

Translation Service

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" example
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" example
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 example
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.

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 try !
<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 try !
<%
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

 

  © Copyright 2003-04. E-Learning Center.   free web templates from myfreetemplates.com