script.dictionary使用方法
创建对象
set objMyData = server.createobject( "scripting.dictionary" )
' 添加项目
objMyData.add "dataKey", "dataValue"
' 获取项目
sDataValue = objMyData.item( "dataKey" )
' 删除项目
sDataValue.remove( "dataKey" )Dictionary 对象与 PERL 关联数组等价。可以是任何形式的数据的条目被存储在数组中。每个条目都与一个唯一的关键字相关联。该关键字用来检索单个条目,通常是整数或字符串,可以是除数组外的任何类型。
Dim a, d, i '创建一些变量
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" '添加一些关键字和条目。
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Items '取得条目
For i = 0 To d.Count -1 '重复数组
Print a(i) '打印条目
Next
4. 设置比较模式<br>
Dictionary的CompareMode属性仅适用于VBScript,不能在JScript中使用。当比较字符串键时,允许指定比较的方式。两个允许的值为BinaryCompare(0)和TextCompare(1)。BinaryCompare(0)为二进制数对照(即区分大小写);TextCompare(1)为文本对照(即不区分大小写)。<br>
5. 遍历Dictionary
研究Dictionary时,有两个方法和一个属性需要特别注意,它们允许我们遍历存储在Dictionary里的所有键/条目对。Items方法用一个一维数组的形式返回Dictionary里所有的条目数据,而keys方法用一个一维数组返回所有已存在的键值。可以使用Count属性得到键或条目的数量 例如,可以使用下列代码得到名称为objMyData的Dictionary中所有的键和条目值。注意,虽然Count属性保存了在Dictionary里的键/条目数量,但VBScript和JScript的数组总是从下标0开始的。因此,数组下标应从0到Count-1 ‘In VBScript:<br>
arrKeys = objMyData.Keys ‘Get all the keys into an array<br>
arrItems = objMyData.Items ‘Get all the items into an array<br>
For intLoop = 0 To objMyData.Count –1 ‘Iterate through the array<br>
StrThisKey = arrKeys(intLoop) ‘This is the key value<br>
StrThisItem = arrItems(intLoop) ‘This is the item (data) value<br>
Next
// In JScript
// Get VB-style arrays using the Keys() and Items() methods
var arrKeys = new VBArray(objMyData.Keys()).toArray();
var arrItems = new VBArray(objMyData.Items()).toArray();
<br>
for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {
// Iterate through the arrays<br>
strThisKey = arrKeys[intLoop]; // This is the key value<br>
strThisItem = arrItems[intLoop]; // This is the item (data) value<br>
}
在VBScript里也可以使用For Each … Next语句完成同样的功能:
‘ Iterate the dictionary as a collection in VBScript<br>
For Each objItem in arrItems<br>
Response.Write objItem & “ = “ & arrItems(objItem) & “<BR>”<br>
Next
set objMyData = server.createobject( "scripting.dictionary" )
' 添加项目
objMyData.add "dataKey", "dataValue"
' 获取项目
sDataValue = objMyData.item( "dataKey" )
' 删除项目
sDataValue.remove( "dataKey" )Dictionary 对象与 PERL 关联数组等价。可以是任何形式的数据的条目被存储在数组中。每个条目都与一个唯一的关键字相关联。该关键字用来检索单个条目,通常是整数或字符串,可以是除数组外的任何类型。
Dim a, d, i '创建一些变量
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" '添加一些关键字和条目。
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Items '取得条目
For i = 0 To d.Count -1 '重复数组
Print a(i) '打印条目
Next
4. 设置比较模式<br>
Dictionary的CompareMode属性仅适用于VBScript,不能在JScript中使用。当比较字符串键时,允许指定比较的方式。两个允许的值为BinaryCompare(0)和TextCompare(1)。BinaryCompare(0)为二进制数对照(即区分大小写);TextCompare(1)为文本对照(即不区分大小写)。<br>
5. 遍历Dictionary
研究Dictionary时,有两个方法和一个属性需要特别注意,它们允许我们遍历存储在Dictionary里的所有键/条目对。Items方法用一个一维数组的形式返回Dictionary里所有的条目数据,而keys方法用一个一维数组返回所有已存在的键值。可以使用Count属性得到键或条目的数量 例如,可以使用下列代码得到名称为objMyData的Dictionary中所有的键和条目值。注意,虽然Count属性保存了在Dictionary里的键/条目数量,但VBScript和JScript的数组总是从下标0开始的。因此,数组下标应从0到Count-1 ‘In VBScript:<br>
arrKeys = objMyData.Keys ‘Get all the keys into an array<br>
arrItems = objMyData.Items ‘Get all the items into an array<br>
For intLoop = 0 To objMyData.Count –1 ‘Iterate through the array<br>
StrThisKey = arrKeys(intLoop) ‘This is the key value<br>
StrThisItem = arrItems(intLoop) ‘This is the item (data) value<br>
Next
// In JScript
// Get VB-style arrays using the Keys() and Items() methods
var arrKeys = new VBArray(objMyData.Keys()).toArray();
var arrItems = new VBArray(objMyData.Items()).toArray();
<br>
for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {
// Iterate through the arrays<br>
strThisKey = arrKeys[intLoop]; // This is the key value<br>
strThisItem = arrItems[intLoop]; // This is the item (data) value<br>
}
在VBScript里也可以使用For Each … Next语句完成同样的功能:
‘ Iterate the dictionary as a collection in VBScript<br>
For Each objItem in arrItems<br>
Response.Write objItem & “ = “ & arrItems(objItem) & “<BR>”<br>
Next

浙公网安备 33010602011771号