Toggle menu
876
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Dictionary class: Difference between revisions

From Catglobe Wiki
Tungocman (talk | contribs)
No edit summary
No edit summary
 
(18 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''Dictionary : The dictionary object'''
{{CGscriptClass_Template
 
|Name=<nowiki>Dictionary</nowiki>
&nbsp;
|Description=<nowiki>The dictionary object</nowiki>
 
|InheritsFrom=object|Constructors=
<span style="color:#a52a2a;">'''Constructors'''</span>
{{CGscriptConstructors_Template|Description=<nowiki>Create a new dictionary with non-argument.</nowiki>}}
 
{{CGscriptConstructors_Template|Parameters={{CGscriptParameters_Template|Type=params AnyType|Name=params AnyType|Description=}}
*<span style="color:#000000;">'''()''' - Create a new dictionary with non-argument.</span>  
|Description=<nowiki>Create new dictionary with the specified key-value pairs.</nowiki>}}
*<span style="color:#000000;">'''(Params&nbsp; AnyType)''' - Create new dictionary with the specified key-value pairs.</span>  
{{CGscriptConstructors_Template|Parameters=
*<span style="color:#000000;">'''(array keys,&nbsp; array values)''' - Create new dictionary with the specified keys and values.</span>  
{{CGscriptParameters_Template|Type=array|Name=<nowiki>keys</nowiki>|Description=<nowiki>keys arrayConstant</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=array|Name=<nowiki>values</nowiki>|Description=<nowiki>values arrayConstant</nowiki>}}
 
|Description=<nowiki>Create new dictionary with the specified keys and values.</nowiki>}}
&nbsp;<span style="color:#a52a2a;">'''Methods'''</span>
|Methods=
 
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>Add</nowiki>|Parameters=
*<span style="color:#000000;">'''bool Add(string key, AnyType value)''' - Adds the specified key and value to the dictionary.</span>  
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Add with specific key</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=object|Name=<nowiki>value</nowiki>|Description=<nowiki>Add with specific value</nowiki>}}
*<span style="color:#000000;">'''bool Add(number key, AnyType value)''' - Adds the specified key and value to the dictionary.</span>  
|Description=<nowiki>Adds the specified key and value to the dictionary. Return false if there is an item with the same key has already been added.</nowiki>}}
*<span style="color:#000000;">'''Empty set_Item(string key, AnyType value)''' - Set an item based on a key and a value.</span>  
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>Add</nowiki>|Parameters=
*<span style="color:#000000;">'''Empty set_Item(number key, AnyType value)''' - Set an item based on a key and a value.</span>  
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>Add with specific key</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=object|Name=<nowiki>value</nowiki>|Description=<nowiki>Add with specific value</nowiki>}}
*<span style="color:#000000;">'''AnyType get_Item(string key)''' - Get an item based on a key.</span>  
|Description=<nowiki>Adds the specified key and value to the dictionary.</nowiki>}}
*<span style="color:#000000;">'''AnyType get_Item(number key)''' - Get an item based on a key.</span>  
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Add</nowiki>|Parameters=
*<span style="color:#000000;">'''AnyType TryGetValue(string key)''' - Gets the value associated with the specified key.</span>  
{{CGscriptParameters_Template|Type=Dictionary|Name=<nowiki>src</nowiki>|Description=<nowiki>Source of keys and values to add</nowiki>}}
*<span style="color:#000000;">'''AnyType TryGetValue(number key)''' - Gets the value associated with the specified key.</span>  
|Description=<nowiki>Adds the all keys and values from src to current.</nowiki>}}
*<span style="color:#000000;">'''bool ContainsKey(string key)''' - Determines whether the dictionary contains the specified key.</span>  
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Clear</nowiki>|Description=<nowiki>Remove all keys and values from the dictionary.</nowiki>}}
*<span style="color:#000000;">'''bool ContainsKey(number key)''' - Determines whether the dictionary contains the specified key.</span>  
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>ContainsKey</nowiki>|Parameters=
*<span style="color:#000000;">'''bool ContainsValue(AnyType value)''' - Determines whether the dictionary contains the specified value.</span>  
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Check with specific key</nowiki>}}
*<span style="color:#000000;">'''AnyType Remove(string key)''' - Remove the value with the specified key from the dictionary.</span>  
|Description=<nowiki>Determines whether the dictionary contains the specified key.</nowiki>}}
*<span style="color:#000000;">'''AnyType Remove(number key)''' - Remove the value with the specified key from the dictionary.</span>  
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>ContainsKey</nowiki>|Parameters=
*<span style="color:#000000;">'''Empty Clear()''' - Remove all keys and values from the dictionary.</span>  
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>Check with specific key</nowiki>}}
*<span style="color:#000000;">'''string ToString()''' - The string representation of the object.</span>  
|Description=<nowiki>Determines whether the dictionary contains the specified key.</nowiki>}}
 
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>ContainsValue</nowiki>|Parameters=
&nbsp;<span style="color:#a52a2a;">'''Properties'''</span>
{{CGscriptParameters_Template|Type=object|Name=<nowiki>value</nowiki>|Description=<nowiki>Add with specific value</nowiki>}}
 
|Description=<nowiki>Determines whether the dictionary contains the specified value.</nowiki>}}
*<span style="color:#000000;">'''number Count HasGetter'''&nbsp; - Gets the number of key/value pairs contained in the dictionary.</span>  
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Foreach</nowiki>|Parameters=
*<span style="color:#000000;">'''array Keys HasGetter'''&nbsp; - Gets a list of keys.</span>  
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>function</nowiki>|Description=<nowiki>A function that takes 2 parameters, the key and the value</nowiki>}}
*<span style="color:#000000;">'''string ObjectTypeName HasGetter'''&nbsp; - The name of the type of object.</span>  
|Description=<nowiki>Run a function for each element.</nowiki>}}
*<span style="color:#000000;">'''array SortedKeys HasGetter'''&nbsp; - Gets a list of keys sorted by current collation.</span>  
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>this[] { get; }</nowiki>|Parameters=
*<span style="color:#000000;">'''TypeInformation TypeInformation HasGetter'''&nbsp; - Get information about this class.</span>  
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>The key used to lookup the value.</nowiki>}}
*<span style="color:#000000;">'''array Values HasGetter'''&nbsp; - Gets a list of values.</span>  
|Description=<nowiki>Get an item based on a key.</nowiki>}}
 
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>this[] { get; }</nowiki>|Parameters=
&nbsp;
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>The key used to lookup the value.</nowiki>}}
 
|Description=<nowiki>Get an item based on a key.</nowiki>}}
<span style="color:#a52a2a;">'''Examples'''</span>
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Inc</nowiki>|Parameters=
 
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Increase the specific keys value</nowiki>}}
&nbsp;
|Description=<nowiki>Increase the value of a value for the given key by 1, creating the entry at 1 if doesnt exists.</nowiki>}}
 
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>KeysWhere</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 2 parameters, the key and the value, and return true/false</nowiki>}}
|Description=<nowiki>Filters keys based on a predicate.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Remove</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Check with specific key</nowiki>}}
|Description=<nowiki>Remove the value with the specified key from the dictionary.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Remove</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>Remove with specific key</nowiki>}}
|Description=<nowiki>Remove the value with the specified key from the dictionary.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Select</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>function</nowiki>|Description=<nowiki>A function that takes 2 parameters, the key and the value, and return some value</nowiki>}}
|Description=<nowiki>Run a function for each element and make array of return values.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>this[] { set; }</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>The key used to lookup the value.</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=object|Name=<nowiki>value</nowiki>|Description=<nowiki>The value to set.</nowiki>}}
|Description=<nowiki>Set an item based on a key and a value.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>this[] { set; }</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>The key used to lookup the value.</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=object|Name=<nowiki>value</nowiki>|Description=<nowiki>The value to set.</nowiki>}}
|Description=<nowiki>Set an item based on a key and a value.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>ToString</nowiki>|Description=<nowiki>The string representation of the object.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>TryGetValue</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Remove with specific key</nowiki>}}
|Description=<nowiki>Gets the value associated with the specified key.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>TryGetValue</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>Remove with specific key</nowiki>}}
|Description=<nowiki>Gets the value associated with the specified key.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>ValuesWhere</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 2 parameters, the key and the value, and return true/false</nowiki>}}
|Description=<nowiki>Filters values based on a predicate.</nowiki>}}
|Properties=
{{CGscriptProperties_Template|ReturnType=int|Name=<nowiki>Count</nowiki>|HasGetter=1|Description=<nowiki>Gets the number of key/value pairs contained in the dictionary.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=array|Name=<nowiki>Keys</nowiki>|HasGetter=1|Description=<nowiki>Gets a list of keys.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=string|Name=<nowiki>ObjectTypeName</nowiki>|HasGetter=1|Description=<nowiki>The name of the type of object.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=array|Name=<nowiki>SortedKeys</nowiki>|HasGetter=1|Description=<nowiki>Gets a list of keys sorted by current collation.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=TypeInformation|Name=<nowiki>TypeInformation</nowiki>|HasGetter=1|Inherited=object|Description=<nowiki>Get information about this class.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=array|Name=<nowiki>Values</nowiki>|HasGetter=1|Description=<nowiki>Gets a list of values.</nowiki>}}
}}
=== Examples ===
<source lang="javascript">
Dictionary d;
Dictionary d;
print(d); // {}
</source><br>


print(d);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;">&nbsp; // {}</span>
<source lang="javascript">
 
&nbsp;
 
Dictionary d = new Dictionary();
Dictionary d = new Dictionary();
print(d);  // {}
</source><br>


print(d);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006400;">&nbsp;&nbsp; // {}</span>
<source lang="javascript">
 
&nbsp;
 
Dictionary d = {"key1": 1, "key2": 2};
Dictionary d = {"key1": 1, "key2": 2};
print(d);  // {"key1": 1, "key2": 2}
</source><br>


print(d);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006400;">// {"key1": 1, "key2": 2}</span>
<source lang="javascript">
 
Dictionary d = {};             // Not work, will cause error.
&nbsp;
</source><br>  
 
Dictionary d = {};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color:#006400;"> // Not work, will cause error.</span>
 
&nbsp;


<source lang="javascript">
Dictionary d1 = new Dictionary("key_01","value01");
Dictionary d1 = new Dictionary("key_01","value01");
 
print(d1);                                                                     // {"key_01": sdasd}
print(d1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006400;"> //&nbsp; {"key_01": sdasd}</span>
 
print(d1.Add("key_02",2222));
print(d1.Add("key_02",2222));
print(d1.Add(3,"hello3333"));
print(d1.Add(3,"hello3333"));
print(d1.Add("key_03",{"happy",48,{"funny","sad",88}}));
print(d1.Add("key_03",{"happy",48,{"funny","sad",88}}));
 
print(d1);                               //         {"3": hello3333, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}}
print(d1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006400;">&nbsp; // &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; {"3": hello3333, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}}</span>
print(d1.ContainsKey("key_01"));                                               // true
 
print(d1.ContainsKey(3));                                                       // true
print(d1.ContainsKey("key_01"));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<span style="color:#006400;"> // true</span>
print(d1.ContainsKey("noexist"));                                               // false
 
print(d1.ContainsValue("value01"));                                             // true
print(d1.ContainsKey(3));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006400;"> // true</span>
print(d1.ContainsValue(2222));                                                 // true
 
print(d1.ContainsValue({"happy",48,{"funny","sad",88}}));                       // true
print(d1.ContainsKey("noexist"));&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// false</span>
print(d1.get_Item("key_01"));                                                   // value01
 
print(d1.get_Item(3));                                                         // hello3333
print(d1.ContainsValue("value01"));&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;<span style="color:#006400;"> // true</span>
print(d1.set_Item("key01",1111));    
 
print(d1.ContainsValue(2222));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span style="color:#006400;">//&nbsp; true</span>
 
print(d1.ContainsValue({"happy",48,{"funny","sad",88}}));&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006400;">// true</span>
 
print(d1.get_Item("key_01"));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<span style="color:#006400;"> // value01</span>
 
print(d1.get_Item(3)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;">&nbsp; // hello3333</span>
 
print(d1.set_Item("key01",1111));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
print(d1.set_Item(3,{3333,"string333"}));
print(d1.set_Item(3,{3333,"string333"}));
print(d1.TryGetValue("key_01"));                                                // value01
print(d1.TryGetValue(3));                                                      // {3333,"string333"}
print(d1);                              //    {"3": {3333,string333}, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}
print(d1.Count);                                                                // 5
print(d1.Keys);                                                                // {key_01,key_02,3,key_03,key01}
print(d1.Values);                                                              // {value01,2222,{3333,string333},{happy,48,{funny,sad,88}},1111}
print(d1.Remove("key_01"));                                                    // value01
print(d1.Remove(3));                                                            // {3333,string333}
print(d1);                                                                      // {"key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}
</source><br />


print(d1.TryGetValue("key_01"));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;"> // value01</span>
<source lang="javascript">
Dictionary d = {
  "key01": "value01",
  5: "value05"
};
print(d["key01"]);      // another way to get the values of keys
print(d[5]);             // another way to get the values of keys
</source> <br>


print(d1.TryGetValue(3));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <span style="color:#006400;">// {3333,"string333"}</span>
<source lang="javascript">
 
//Example for using d.Inc("key") to increase value of the key up to 1, if the key not exist, create the key with value 1
print(d1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;">&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; {"3": {3333,string333}, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}</span>
Dictionary d = {"key1": 1, "key2": 2};
 
d.Inc("key2");// key2 existed, so increate value of key2 to 1 (2->3)
print(d1.Count);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// 5</span>
print(d);//{"key1": 1, "key2": 3}
 
d.Inc("key3");// key3 not exist, so add key3 to d with value 1
print(d1.Keys);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color:#006400;"> &nbsp; // {key_01,key_02,3,key_03,key01}</span>
print(d);//"key1": 1, "key2": 3, "key3": 1}
 
</source>
print(d1.Values);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006400;"> // {value01,2222,{3333,string333},{happy,48,{funny,sad,88}},1111}</span>
 
print(d1.Remove("key_01"));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006400;">// value01</span>
 
print(d1.Remove(3));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006400;">&nbsp; // {3333,string333}</span>
 
print(d1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006400;">&nbsp; // {"key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}</span>
 
&nbsp;


&nbsp;
<source lang="javascript">
//Use KeysWhere/ValuesWhere to get list of keys/values meet filter
Dictionary d = {"key1":1,"key2":2,"key3":3,"key4":4};
//KeysWhere
d.KeysWhere(function(string key, number value ){if (value >= 3) return true;});// result: {key3,key4}
//ValuesWhere
d.ValuesWhere(function(string key, number value ){if (value >= 3) return true;});// result: {3,4}


&nbsp;
</source>


[[Category:Data_Types_Literals_and_Variables]]
[[Category:Classes]]

Latest revision as of 05:28, 20 July 2020

Dictionary



The dictionary object

Parent class

Inherits from object

Constructors

  • () - Create a new dictionary with non-argument.
  • (params AnyType) - Create new dictionary with the specified key-value pairs.
  • (array keys "keys arrayConstant", array values "values arrayConstant") - Create new dictionary with the specified keys and values.

Methods

  • bool Add(string key "Add with specific key", object value "Add with specific value") - Adds the specified key and value to the dictionary. Return false if there is an item with the same key has already been added.
  • bool Add(int key "Add with specific key", object value "Add with specific value") - Adds the specified key and value to the dictionary.
  • Empty Add(Dictionary src "Source of keys and values to add") - Adds the all keys and values from src to current.
  • Empty Clear() - Remove all keys and values from the dictionary.
  • bool ContainsKey(string key "Check with specific key") - Determines whether the dictionary contains the specified key.
  • bool ContainsKey(int key "Check with specific key") - Determines whether the dictionary contains the specified key.
  • bool ContainsValue(object value "Add with specific value") - Determines whether the dictionary contains the specified value.
  • Empty Foreach(Function function "A function that takes 2 parameters, the key and the value") - Run a function for each element.
  • object this[] { get; }(string key "The key used to lookup the value.") - Get an item based on a key.
  • object this[] { get; }(int key "The key used to lookup the value.") - Get an item based on a key.
  • Empty Inc(string key "Increase the specific keys value") - Increase the value of a value for the given key by 1, creating the entry at 1 if doesnt exists.
  • array KeysWhere(Function predicate "A function that takes 2 parameters, the key and the value, and return true/false") - Filters keys based on a predicate.
  • object Remove(string key "Check with specific key") - Remove the value with the specified key from the dictionary.
  • object Remove(int key "Remove with specific key") - Remove the value with the specified key from the dictionary.
  • array Select(Function function "A function that takes 2 parameters, the key and the value, and return some value") - Run a function for each element and make array of return values.
  • Empty this[] { set; }(string key "The key used to lookup the value.", object value "The value to set.") - Set an item based on a key and a value.
  • Empty this[] { set; }(int key "The key used to lookup the value.", object value "The value to set.") - Set an item based on a key and a value.
  • string ToString() - The string representation of the object.
  • object TryGetValue(string key "Remove with specific key") - Gets the value associated with the specified key.
  • object TryGetValue(int key "Remove with specific key") - Gets the value associated with the specified key.
  • array ValuesWhere(Function predicate "A function that takes 2 parameters, the key and the value, and return true/false") - Filters values based on a predicate.

Properties

  • int Count { get; } - Gets the number of key/value pairs contained in the dictionary.
  • array Keys { get; } - Gets a list of keys.
  • string ObjectTypeName { get; } - The name of the type of object.
  • array SortedKeys { get; } - Gets a list of keys sorted by current collation.
  • (From object) TypeInformation TypeInformation { get; } - Get information about this class.
  • array Values { get; } - Gets a list of values.

Examples

Dictionary d;
print(d); // {}


Dictionary d = new Dictionary();
print(d);  // {}


Dictionary d = {"key1": 1, "key2": 2};
print(d);   // {"key1": 1, "key2": 2}


Dictionary d = {};             // Not work, will cause error.


Dictionary d1 = new Dictionary("key_01","value01");
print(d1);                                                                      // {"key_01": sdasd}
print(d1.Add("key_02",2222));
print(d1.Add(3,"hello3333"));
print(d1.Add("key_03",{"happy",48,{"funny","sad",88}}));
print(d1);                               //          {"3": hello3333, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}}
print(d1.ContainsKey("key_01"));                                                // true
print(d1.ContainsKey(3));                                                       // true
print(d1.ContainsKey("noexist"));                                               // false
print(d1.ContainsValue("value01"));                                             // true
print(d1.ContainsValue(2222));                                                  // true
print(d1.ContainsValue({"happy",48,{"funny","sad",88}}));                       // true
print(d1.get_Item("key_01"));                                                   // value01
print(d1.get_Item(3));                                                          // hello3333
print(d1.set_Item("key01",1111));     
print(d1.set_Item(3,{3333,"string333"}));
print(d1.TryGetValue("key_01"));                                                // value01
print(d1.TryGetValue(3));                                                       // {3333,"string333"}
print(d1);                              //     {"3": {3333,string333}, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}
print(d1.Count);                                                                // 5
print(d1.Keys);                                                                 // {key_01,key_02,3,key_03,key01}
print(d1.Values);                                                               // {value01,2222,{3333,string333},{happy,48,{funny,sad,88}},1111}
print(d1.Remove("key_01"));                                                     // value01
print(d1.Remove(3));                                                            // {3333,string333}
print(d1);                                                                      // {"key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}


Dictionary d = {
  "key01": "value01",
  5: "value05"
}; 
print(d["key01"]);       // another way to get the values of keys
print(d[5]);             // another way to get the values of keys


//Example for using d.Inc("key") to increase value of the key up to 1, if the key not exist, create the key with value 1
Dictionary d = {"key1": 1, "key2": 2};
d.Inc("key2");// key2 existed, so increate value of key2 to 1 (2->3)
print(d);//{"key1": 1, "key2": 3}
d.Inc("key3");// key3 not exist, so add key3 to d with value 1
print(d);//"key1": 1, "key2": 3, "key3": 1}
//Use KeysWhere/ValuesWhere to get list of keys/values meet filter
Dictionary d = {"key1":1,"key2":2,"key3":3,"key4":4};
//KeysWhere
d.KeysWhere(function(string key, number value ){if (value >= 3) return true;});// result: {key3,key4}
//ValuesWhere
d.ValuesWhere(function(string key, number value ){if (value >= 3) return true;});// result: {3,4}