`

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

分享到:
评论
1 楼 八月约克 2011-12-23  
火狐不支持这个东东

相关推荐

    VB Scripting.Dictionary

    Scripting.Dictionary VB Scripting.Dictionary VB

    asp中Scripting.Dictionary字典对象使用示例

    VBScript中Scripting.Dictionary使用示例如下: 代码如下: Dim objDict Set objDict = WSH.CreateObject(“Scripting.Dictionary”)  ‘ .Add(key, value)   objDict.Add “a”, “value1”  objDict.Add “b”,...

    ActiveX 部件不能创建对象 Scripting.Dictionary解决办法

    ActiveX 部件不能创建对象 Scripting.Dictionary解决办法

    带进度传上传文件

    Set Files = Server.CreateObject("Scripting.Dictionary") ' 上传文件集合 Set Form = Server.CreateObject("Scripting.Dictionary") ' 表单集合 UploadProgressInfo = "DoteyUploadProgressInfo" ' ...

    AutoItDict2:AutoIt 的字典类型的实现

    一直使用Scripting.dictionary很费力。 Python dict API 优于Scripting.dictionary的 例子 #include #include $dict = _DictCreate() ConsoleWrite ( $dict .len()) ; Outputs 0 $dict .set( " key1 " , " ...

    字典基础(你可能不会字典!)

    '1.关于字典 '字典: 是为字词提供音韵、意思...'Set d = CreateObject("scripting.dictionary") '建议写代码时做前期绑定,写完之后改成后期绑定. Sub test() Set d = CreateObject("scripting.dictionary") End Sub

    Javascript.Developers.Dictionary

    JavaScript. The term itself implies a scripting language, a “patchwork” for whatever Web page you have in mind. But, like many scripting languages, it’s not necessarily a simple one.

    6ksu forum v1.0(6k美化版)

    服务器要求: 数据库access2000以上 所需组件:adodb.recordset 必须 adodb.connection必须 scripting.filesystemobject 可选 adodb.stream Scripting.Dictionary 可选

    asp操作数据库例子

    &lt;!--#include file="include/conn....set sd=server.createobject("scripting.dictionary") sd.item("zc_name")="肖西锋" sd.item("zc_sex")="男" sd.item("zc_age")="20" call dry_insert("zc_test",sd) %&gt; &lt;/html&gt;

    dictionary:包含我们在 Grace 中对 Dictionary 对象的实现以及一套测试

    字典包含我们在 Grace 中对 Dictionary 对象的实现以及一组测试。

    字典语法基础

    Set d = CreateObject("scripting.Dictionary") d.Add "张三", "123" d.Add "李四", "456" '对象.新增 Key,Item '注意:在本地窗口中可能看不到Item条件。 'Keys方法 '返回一个数组,其中包含了一个 Dictionary ...

    企业网站管理系统 v6.zip

    5、组 件:Scripting.Dictionary (通常都支持) 6、浏览器:IE5.5以上版本,开启Cookies (通常都支持)   二、注意事项: 1、请在IIS环境下安装本系统,不支持使用其他绿色集成软件; 2、网站所在目录必须具备...

    风声 ASP 无组件上传类应用实例

    风声 ASP 无组件上传类应用实例,配置实例: Private Sub Class_Initialize m_Error = -1 ... Set m_dicForm = Server.CreateObject("Scripting.Dictionary") m_dicForm.CompareMode = 1 End Sub

    asp 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 ...

    计算重复属性

    Static d As Object Static i As Long ...Set d = CreateObject("Scripting.Dictionary") End If If (d.Exists(CStr(sField))) Then iDup = 1 Else d.Add CStr(sField), 1 iDup = 0 End If i = i + 1

    小学三科成绩统计表.xlsm

    三科成绩统计表 Sub aaa() If Sheets.Count &gt; 2 Then For i = 3 To Sheets.Count Application.DisplayAlerts = 0 ...Set d = CreateObject("Scripting.Dictionary") ………………………………………………

    订单管理代码

    Private Sub Worksheet_Activate() Dim d, arr, brr(), s$, i,... Set d = CreateObject("Scripting.Dictionary") arr = Sheet2.[A1].CurrentRegion ReDim brr(1 To UBound(arr), 1 To 8) For i = 2 To UBound(arr)

    非常完善功能非常好的网站

    '由于Scripting.Dictionary区分大小写,所以在网页及ASP页的项目名都要相同的大小 '写,如果人习惯用大写或小写,为了防止出错的话,可以把 'sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart) '改为 '(小写者)...

    易语言高性能哈希表模块和例程

    最近做项目需要用到哈希表,由于...像“闪电哈希”,“scripting.Dictionary”对象 伪哈希 。不是速度太慢 就是功能不全 。本哈希类为模仿java中 jdk哈希表。只是处理冲突键时,一直使用的线性表。@5182235367。

Global site tag (gtag.js) - Google Analytics