5.3.1 創建和使用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
// 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