Scripting.Dictionary對象使用說明
建立和使用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>
1. Dictionary對象的成員概要
表5-2和表5-3列出了Dictionary對象的屬性和方法及相應的說明。
當增加一個鍵/條目對時,如果該鍵已存在;或者删除一個鍵/條目對時,該關鍵字/條目對不存在,或改變已包含資料的Dictionary對象的CompareMode,都将産生錯誤。
表5-2 Dictionary對象的屬性和說明
屬 性 | 說 明 |
CompareMode | (僅用于VBScript)設定或傳回鍵的字元串比較模式 |
Count | 隻讀。傳回Dictionary裡的鍵/條目對的數量 |
表5-3 Dictionary對象的方法和說明
方 法 | 說 明 |
Item(key) | 設定或傳回指定的鍵的條目值 |
Add(key,item) | 增加鍵/條目對到Dictionary |
Exists(key) | 如果指定的鍵存在,傳回True,否則傳回False |
Items() | 設定或傳回指定的鍵的條目值 |
方 法 說 明
Add(key,item) 傳回一個包含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
//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();
for (intLoop = 0; intLoop < objMyData.Count; intLoop++)
{
// Iterate through the arrays
strThisKey = arrKeys[intLoop];
//This is the key value
strThisItem = arrItems[intLoop];
// This is the item (data) value
}
在VBScript裡也可以使用For Each … Next語句完成同樣的功能:
' Iterate the dictionary as a collection in VBScript
For Each objItem in arrItems
Response.Write objItem & " = " & arrItems(objItem) & "<BR>"
Next