Scripting.dictionary object

xiaoxiao2021-03-06  38

Many Microsoft's programming languages, such as Visual Basic, VBScript, and JScript, provide a collection. You can imagine a collection as an array, you can use basic tasks such as stored and manipulated data using the built-in functions. No need to worry about which ranks are in, but use the unique key to access.

Both VBScript and JScript provide similar objects, which are generally referred to as scripting.dictionary objects or Dictionary objects. It is similar to the two-dimensional array, putting the data of the key and related entries together. However, the true object-oriented method should not directly access the data entry, and must be implemented using the methods and properties supported by Dictionary objects.

This chapter provides some sample pages that allow the method and properties of the test script running period object. These examples in the chaper05 subdirectory of the file of the downloaded file.

5.3.1 Creating and using Dictionary objects

Examples of creating a Dictionary object are as follows:

'In Vbscript:

DIM Objmydata

Set objmydata = server.createObject ("scripting.dictionary")

// in jscript:

Var objmydata = server.createObject ('scripting.dictionary');

Progid = "scripting.dictionary">

Dictionary objects can also be used in the client's IE.

1. Summary of members of Dictionary objects

Tables 5-2 and Table 5-3 list the properties and methods of the Dictionary object and the corresponding description.

When a key / entry is added, if the key already exists; or when a key / entry is deleted, the keyword / entry does not exist, or change the COMPAREMODE of the Dictionary object that has the data containing the data, it will generate an error.

Table 5-2 Attributes and descriptions of Dictionary objects

Attribute

Comparemode (VBScript only for VBScript) setting or return string comparison mode

COUNT is read only. Returns the number of keys / entries in Dictionary

Item (key) setting or return the entry value of the specified key

Key setting key value

Table 5-3 Method and description of Dictionary object

Method

Add (Key, Item) Add key / entry to Dictionary

EXISTS (Key) If the specified key exists, return true, otherwise return false

Items () returns an array of all entries in the Dictionary object

Keys () Returns an array containing all keys in the Dictionary object

REMOVE (Key) Delete a specified key / entry

Removeall () Delete all keys / entries

2. Added and deleting entries for Dictionary

Once you get a new (empty) Dictionary, you can add an entry to get your entry and delete entries:

'In Vbscript:

Objmydata.add "mykey", "myitem" 'add value myitem with key mykey

Objmydata.add "Yourkey", "YourItem" 'add value YourItem with key yourkeyblnisthere = 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 item

In JScript, the equivalent code is:

// 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. Modify the key or entries

You can change the data stored in Dictionary by modifying the value of the key or by modifying the entry associated with a particular key. The following code changes the data in the entries of the key to myKey.

Objmydata.item ("mykey") = "newValue" 'in VBScript

ObjmyData.item ('mykey') = 'newValue'; // in jscript

If the specified key is not found in Dictionary, create a Mykey as a key in Dictionary, a new key / entry to NEW VALUE for its entry value. Interestingly, if you use a non-existing key to retrieve entries, not only get an empty string (this is ideal), but also add a new key / entry pair in Dictionary, the key is the specified key But the data of the entry is empty.

You can use the key attribute only to change the value of the key without changing the data corresponding to the entry. Change an existing key mykey to MyNewkey, you can use:

Objmydata.key ("mykey") = "MyNewValue" 'in VBScript

Objmydata.item ('mykey') = 'mynewvalue'; // in jscript

If the specified key is not found, a running period error is generated.

4. Set comparison mode

Dictionary's CompareMode property is only available for VBScript and cannot be used in JScript. When comparing the string keys, the specified comparison is allowed. Two allowed values ​​are binarycompare (0) and TextCompare (1). BinaryCompare (0) is a binary comparison (ie case sensitive); TextCompare (1) is a text control (ie, case sensitive). 5. Traverse Dictionary

When studying Dictionary, there are two ways and one attribute to take special attention, which allow us to traverse all keys / entries stored in Dictionary. The Items method returns all item data in Dictionary in the form of a one-dimensional array, and the Keys method returns all existing key values ​​in a one-dimensional array. You can use the count property to get the number of keys or entries.

For example, you can use the following code to get all the keys and entries in the Dictionary named ObJMYDATA. Note that although the count property saves the number of key / entry in Dictionary, the array of VBScript and JScript is always starting from the subscript 0. Therefore, the array should be taken from 0 to count-1.

'In Vbscript:

Arrkeys = ObjmyData.keys' get all the keys into 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

// iprate through the arrays

StRTHISKEY = arrkeys [inTloop]; // this is the key value

StRTHISITEM = arritems [inTloop]; // this is the item (data) Value

}

In VBScript, you can also use the for Each ... next statement to complete the same function:

'Itereate The Dictionary as a Collection in Vbscript

For Each Objitem in Arritems

Response.write Objitem & "=" & Arritems (Objitem) & "
"

NEXT

5.3.2 Dictionary Object Example

This book provides a range of sample files to test the various properties of the script running time library.

The default page of this chapter provides a series of VBScript example links available. Some examples are equally valid for JScript. These examples are stored in the corresponding subdirectory in the CHAPTER05 directory, and the displayed interface is shown in Figure 5-2: To view the runaryary object, click on the first link in the menu page, open the page named show_dictionary.asp. This page shows the content of the Dictionary object we provide, allowing you to test its properties and methods. The screen is shown in Figure 5-3:

1. Dictionary's global.asa file

One of the files provided with the Dictionary Object Sample page is Global.asa. It creates and pre-filled a Dictionary object of a session layer scope, so its content will not be lost between the page request. Generally speaking (considering scalability), this is not an ideal approach. In this example, you can see the effects of Dictionary's properties and methods.

If you download and install an example on your own server, you must create a virtual application based on this global.asa file. Or add its content to the Global.asa file in the root folder of the default site. In Chapter 3, how to create a virtual application with a wizard. However, for this example, the easiest way to create a virtual application is to right-click the Dictionary subfolder in the Chapter05 sample folder, in the Home Directory tab of the Properties dialog, click the Create button, as shown in Figure 5-4 :

In this global.asa file, the code uses the element to create a Scripting.Dictionary object instance of a session layer scope. Then put a series of values ​​in Dictionary in the session_onstart event handler, and specify a reference to the Dictionary object to the ASP session variable myDictionary:

Progid = "scripting.dictionary">