scripting.dictionary的彻底研究

set Font=server.createobject("Scripting.Dictionary")

forn.add("键名",a)

a可以是变量,也可以是数组变量。

创建和使用Dictionary对象
创建一个Dictionary对象的示例如下:
‘In VBScript:
Dim objMyData
Set objMyData = Server.CreateObject(“Scripting.Dictionary”)

//In Jscript:
var objMyData = Server.CreateObject(‘Scripting.Dictionary’);

<!-- Server-Side with an OBJECT element -->
<OBJECT RUNAT=”SERVER” SCOPE=”PAGE” ID=”objMyData”
         PROGID=”Scripting.Dictionary”>
</OBJECT>
Dictionary对象还可用于客户端的IE
1. Dictionary对象的成员概要
表5-2和表5-3列出了Dictionary对象的属性和方法及相应的说明。
当增加一个键/条目对时,如果该键已存在;或者删除一个键/条目对时,该关键字/条目对不存在,或改变已包含数据的Dictionary对象的CompareMode,都将产生错误。
表5-2   Dictionary对象的属性和说明
属 性 说 明
CompareMode (仅用于VBScript)设定或返回键的字符串比较模式
Count 只读。返回Dictionary里的键/条目对的数量
Item(key) 设定或返回指定的键的条目值
Key(key) 设定键值
表5-3   Dictionary对象的方法和说明
方 法 说 明
Add(key,item) 增加键/条目对到Dictionary
Exists(key) 如果指定的键存在,返回True,否则返回False
Items() 返回一个包含Dictionary对象中所有条目的数组
Keys() 返回一个包含Dictionary对象中所有键的数组
Remove(key) 删除一个指定的键/条目对
RemoveAll() 删除全部键/条目对
2. 对Dictionary中增加和删除条目
一旦得到一个新的(空的)Dictionary,可以对其添加条目,从中获取条目以及删除条目:
‘ In VBScript:
objMyData.Add “MyKey”, “MyItem”          ‘Add Value MyItem with key MyKey
objMyData.Add “YourKey”, ”YourItem”        ‘Add value YourItem with key YourKey
blnIsThere = objMyData.Exists(“MyKey”)        ‘Returns True because the item exists
strItem = objMyData.Item(“YourKey”)          ‘Retrieve value of YourKey
strItem = objMyData.Remove(“MyKey”)        ‘Retrieve and remove YourKey
objMyData.RemoveAll                      ‘Remove all the items
在JScript中,等价的代码为:
// In JScript;
objMyData.Add (‘MyKey’,‘MyItem’);          //Add Value MyItem with key MyKey
objMyData.Add (‘YourKey’, ‘YourItem’);        //Add value YourItem with key YourKey
var blnIsThere = objMyData.Exists(‘MyKey’); //Returns True because the item exists
var strItem = objMyData.Item(‘YourKey’);        //Retrieve value of YourKey
var strItem = objMyData.Remove(‘MyKey’); //Retrieve and remove YourKey
objMyData.RemoveAll();                    //Remove all the items
3. 修改键或条目的值
可以通过修改键的值,或通过修改与特定的键关联的条目的数据,来改变存储在Dictionary内的数据。下面的代码改变键为MyKey的条目中的数据。
ObjMyData.Item(“MyKey”) = “NewValue”        ‘ In VBScript
ObjMyData.Item(‘MyKey’) = ‘NewValue’;        // In JScript
如果指定的键在Dictionary未找到,将在Dictionary中创建一个以MyKey为键,以New Value为其条目值的新的键/条目对。有意思的是,如果使用一个不存在的键来检索条目,不仅得到一个空的字符串(这是可以想到的),而且还在Dictionary里添加一个新的键/条目对,键即是指定的键,但条目的数据为空。
可以使用Key属性仅改变键的值而不改变与之对应的条目的数据。将一个已存在的键MyKey改变为MyNewKey,可以用:
objMyData.Key(“MyKey”) = “MyNewValue”        ‘ In VBScript
objMyData.Item(‘MyKey’) = ‘MyNewValue’;        // In JScript
如果指定的键未找到,则产生运行期错误。
4. 设置比较模式
Dictionary的CompareMode属性仅适用于VBScript,不能在JScript中使用。当比较字符串键时,允许指定比较的方式。两个 允许的值为BinaryCompare(0)和TextCompare(1)。BinaryCompare(0)为二进制数对照(即区分大小写); TextCompare(1)为文本对照(即不区分大小写)。
5. 遍历Dictionary
研究Dictionary时,有两个方法和一个属性需要特别注意,它们允许我们遍历存储在Dictionary里的所有键/条目对。Items方法用一个 一维数组的形式返回Dictionary里所有的条目数据,而keys方法用一个一维数组返回所有已存在的键值。可以使用Count属性得到键或条目的数 量。
例如,可以使用下列代码得到名称为objMyData的Dictionary中所有的键和条目值。注意,虽然Count属性保存了在Dictionary 里的键/条目数量,但VBScript和JScript的数组总是从下标0开始的。因此,数组下标应从0到Count-1。
‘In VBScript:
arrKeys = objMyData.Keys                    ‘Get all the keys into an array
arrItems = objMyData.Items                    ‘Get all the items into an array

For intLoop = 0 To objMyData.Count –1        ‘Iterate through the array
StrThisKey = arrKeys(intLoop)          ‘This is the key value
StrThisItem = arrItems(intLoop)          ‘This is the item (data) value
Next

var arrKeys = new VBArray(objMyData.Keys()).toArray();
var arrItems = new VBArray(objMyData.Items()).toArray();

for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {
    strThisKey = arrKeys[intLoop];          // This is the key value
strThisItem = arrItems[intLoop];          // This is the item (data) value
}
在VBScript里也可以使用For Each … Next语句完成同样的功能:

For Each objItem in arrItems
Response.Write objItem & “ = “ & arrItems(objItem) & “<BR>”
Next

====================================================

关于script的dictionary对象,其实我想一开始ms是借鉴了python之类的动态脚本语言的,而且是面对客户端开发的关于这一点可以参考wrox的asp程序员参考手册里第137页里,如果在session级保存一个dictionary对象会降低系统的性能,而在application级保存一个dictionary对象会导致web服务器崩溃,关于这个就不在多说了。

现在我们要考虑的是dictionary对象在单页的时候,有哪些设计时的缺陷:

大家可以这么试试

set rs=server.createobject("adodb.recordset")

sql="select * from table"

rs.open sql,conn,1,3

set ttt=server.createobject("scripting.dictionary")

ttt.add "xxx",rs("field")

set rs=nothing

liu=ttt("xxx") '当你这么做的时候会发现一件什么事呢?asp页会告诉你发生意外!!!这个就很令人诧异了,什么是意外呢?很难说,后来经过我反复的测试发现是因为不能把rs("field")的值直接给dictionary对象,经过反复研究得出的结论是如下的:dictionry是把rs("field")的内存地址给储存了,这样的话,我刚才写的那个无疑是一种灾难,解决方法是把这个rs("field")放到一个变量里就可以解决了,但是dictionary对象难道不可以保存一个被nothing过的对象么?这个就是一个很大的疑问了,所以我又写了这么一段程序,大家可以保存成try.htm看看效果的

<script language='vbscript'>
set ttt=createobject("scripting.dictionary")
ttt.add "liu","uuu"
set ddd=createobject("scripting.dictionary")
ddd.add "ppp",ttt
set ttt=nothing
bbb=ddd("ppp")("liu")
alert(bbb)
</script>

结果是什么?你会发现一段alert了uuu说明是没有问题的,这就说明dictionary对象其实是把另一个dictionary对象整个clone进来了,这就更验证了wrox的书中写的,dictionary对象在ms开发的时候其实是针对客户端的,这种说法了

还有对数组与dictionary合用的代码也可以给大家看看的

<script language='vbscript'>
i=1
picname=("xxx")
str="set " & "pic_" & i & "=createobject(" & """" & "scripting.dictionary" & """" & ")"
execute(str)
str="pic_" & i & ".add " & """" & "picname" & """" & "," & """" & picname & """"
execute(str)
dim ttt()
redim ttt(5)
ttt(0)="uuu"
pic_1.add "item",ttt
liu=pic_1("picname")

set pic_2=createobject("scripting.dictionary")
erase ttt
redim ttt(5)
ttt(0)="iii"
nnn=pic_1("item")(0)
pic_2.add "rrr",ttt
zzz=pic_2("rrr")(0)
alert(liu)
alert(nnn)
alert(zzz)
</script>

 

申明

非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

博文欢迎转载,但请给出原文连接。

原文地址:https://www.cnblogs.com/Athrun/p/1177180.html