[asp]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 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章