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.

Array class: Difference between revisions

From Catglobe Wiki
Tungocman (talk | contribs)
No edit summary
No edit summary
 
(41 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<p style="color:#000099; font-size:14px;"><strong>Array : The array object</strong></p>
{{CGscriptClass_Template
&nbsp;
|Name=<nowiki>Array</nowiki>
|Description=<nowiki>The array object</nowiki>
|InheritsFrom=object|Constructors=
{{CGscriptConstructors_Template|Description=<nowiki>Creates an empty array</nowiki>}}
{{CGscriptConstructors_Template|Parameters=
{{CGscriptParameters_Template|Type=array|Name=<nowiki>toCopy</nowiki>|Description=<nowiki>Array to copy</nowiki>}}
|Description=<nowiki>Creates a copy of another array</nowiki>}}
{{CGscriptConstructors_Template|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>count</nowiki>|Description=<nowiki>Number of items to put in the array</nowiki>}}
|Description=<nowiki>Creates an array with a predefined size, where all elements are Empty</nowiki>}}
|Methods=
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Add</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=object|Name=<nowiki>element</nowiki>|Description=<nowiki>element to add</nowiki>}}
|Description=<nowiki>Add a new element to the Array</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>AddRange</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=array|Name=<nowiki>source</nowiki>|Description=<nowiki>Array to get elements from.</nowiki>}}
|Description=<nowiki>Add all elements of another array to this array.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>Any</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 1 parameter of the types in the array, and return true/false</nowiki>}}
|Description=<nowiki>Return true if the array contains the element using the given function.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Clear</nowiki>|Description=<nowiki>Remove all items in array</nowiki>}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>Contains</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=object|Name=<nowiki>element</nowiki>|Description=<nowiki>Element to check for</nowiki>}}
|Description=<nowiki>Return true if the array contains the element using the normal equal operator.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Except</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=array|Name=<nowiki>elements</nowiki>|Description=<nowiki>Elements to remove</nowiki>}}
|Description=<nowiki>Return all elements that does not exist in the other collection.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>First</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A function to test each element for a condition.</nowiki>}}
|Description=<nowiki>Returns the first element in a sequence that satisfies a specified condition.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>FirstOrDefault</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A function to test each element for a condition.</nowiki>}}
|Description=<nowiki>Returns the first element in a sequence that satisfies a specified condition or empty if not found.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Foreach</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>function</nowiki>|Description=<nowiki>A function that takes 1 parameter of the types in the array, and an optional 2nd parameter that is the index</nowiki>}}
|Description=<nowiki>Filters a sequence of values based on a predicate.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=int|Name=<nowiki>Frequency</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>number</nowiki>|Description=<nowiki>The number to search for</nowiki>}}
|Description=<nowiki>Counts the number of times a given Number object exists in the Array. Can only use if all the elements are of type Number</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>this[] { get; }</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>index</nowiki>|Description=<nowiki>Index</nowiki>}}
|Description=<nowiki>Get element at index</nowiki>}}
{{CGscriptMethods_Template|ReturnType=int|Name=<nowiki>IndexOf</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=object|Name=<nowiki>element</nowiki>|Description=<nowiki>Element to search for</nowiki>}}
|Description=<nowiki>Return index of the given element using ==, or -1.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=int|Name=<nowiki>IndexOfObject</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=object|Name=<nowiki>element</nowiki>|Description=<nowiki>Element to search for</nowiki>}}
|Description=<nowiki>Return index of the given element using references, or -1.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsCharacterArray</nowiki>|Description=<nowiki>check if array is an array of characters</nowiki>}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsNumericArray</nowiki>|Description=<nowiki>check if array is an array of integer numbers</nowiki>}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsStringArray</nowiki>|Description=<nowiki>check if array is an array of string</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>OrderBy</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>comparer</nowiki>|Description=<nowiki>Function that compares two objects of the same type. Must return a signed integer that indicates the relative values of first param A and second param B. Value Less than 0 : A is less than B.Value 0 : A equals B.Value Greater than 0 : A is greater than B.It can also be a function that takes 1 parameter and returns a string or number.</nowiki>}}
|Description=<nowiki>Sorts the elements of a sequence in ascending order by using a specified comparer.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Randomize</nowiki>|Description=<nowiki>OBSOLETE! Randomize the order of the elements in the current array. Multiple calls to this may return the same randomization, use a Random object to preserve randomization over multiple calls</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Randomize</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Random|Name=<nowiki>random</nowiki>|Description=<nowiki>Random object</nowiki>}}
|Description=<nowiki>Randomize the order of the elements in the current array.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Remove</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=object|Name=<nowiki>item</nowiki>|Description=<nowiki>Item will be removed</nowiki>}}
|Description=<nowiki>Removes the first occurrence of an element from the Array</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>RemoveItemAt</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>index</nowiki>|Description=<nowiki>The index to remove the object from</nowiki>}}
|Description=<nowiki>Remove an element from the Array by its index</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Reverse</nowiki>|Description=<nowiki>Returns an array with all the elements in the opposite order.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Select</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A transform function to apply to each element.</nowiki>}}
|Description=<nowiki>Projects each element of a sequence into a new form.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>SelectMany</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A transform function to get each sub array.</nowiki>}}
|Description=<nowiki>Projects each array element of a sequence into a new form.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>this[] { set; }</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>index</nowiki>|Description=<nowiki>Index</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=object|Name=<nowiki>value</nowiki>|Description=<nowiki>Value to set</nowiki>}}
|Description=<nowiki>Set element value at index</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Skip</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>n</nowiki>|Description=<nowiki>How many elements to skip</nowiki>}}
|Description=<nowiki>Get all but the n first elements.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Take</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=int|Name=<nowiki>n</nowiki>|Description=<nowiki>How many elements to take</nowiki>}}
|Description=<nowiki>Get the n first elements.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Dictionary|Name=<nowiki>ToDictionary</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>keySelector</nowiki>|Description=<nowiki>A transform function to get the key of each element.</nowiki>}}
|Description=<nowiki>Return a dictionary with the elements of the array.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=Dictionary|Name=<nowiki>ToDictionary</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>keySelector</nowiki>|Description=<nowiki>A transform function to get the key of each element.</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=Function|Name=<nowiki>valueSelector</nowiki>|Description=<nowiki>A transform function to get the value of each element.</nowiki>}}
|Description=<nowiki>Return a dictionary with the elements of the array.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>ToString</nowiki>|Description=<nowiki>The string representation of the object.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Where</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 1 parameter of the types in the array, and an optional 2nd parameter that is the index and return true/false</nowiki>}}
|Description=<nowiki>Filters a sequence of values based on a predicate.</nowiki>}}
|Properties=
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Average</nowiki>|HasGetter=1|Description=<nowiki>Average of the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
{{CGscriptProperties_Template|ReturnType=int|Name=<nowiki>Count</nowiki>|HasGetter=1|Description=<nowiki>Number of elements in Array</nowiki>}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Max</nowiki>|HasGetter=1|Description=<nowiki>Largest of all the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Min</nowiki>|HasGetter=1|Description=<nowiki>Smallest of all the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
{{CGscriptProperties_Template|ReturnType=string|Name=<nowiki>ObjectTypeName</nowiki>|HasGetter=1|Description=<nowiki>The name of the type of object.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Sum</nowiki>|HasGetter=1|Description=<nowiki>Sum of all the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
{{CGscriptProperties_Template|ReturnType=TypeInformation|Name=<nowiki>TypeInformation</nowiki>|HasGetter=1|Inherited=object|Description=<nowiki>Get information about this class.</nowiki>}}
}}


<span style="color:#a52a2a;">'''Constructors'''</span>  
=== <span style="color:#DF8621">'''Examples'''</span> ===


*<span style="color:#000000;">'''() '''- Creates an empty array.</span>  
<source lang="javascript">array x = new Array();
*<span style="color:#000000;">'''(number count)''' - Creates an array with a predefined size, where all elements are Empty. Count is a number type, it is number of items to put in the array.</span>
print(x); // {}</source><br/><source lang="javascript">array x = new Array(10);
print(x); // {Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty}</source><br/><source lang="javascript">array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { a;})
// .Select(function (string a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'String'
// .Select(function (array a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'Array'
// .Select(function (bool a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'Boolean'
// .Select(function () { a;}) -> will not work: Function called with too many parameters @
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;});
print(y);</source><br/><source lang="javascript">array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { 88888;})
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;});
print(y);</source><br/><source lang="javascript">array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { a;})
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;})
.OrderBy(function(number a, number b) {if (a>b) 1; else if (a<b) -1; else 0;});
// .OrderBy(function(number a, number b){if (a>b) -1; else if (a<b) 1; else 0;});
print(y);</source><br/><source lang="javascript">array x = {{1,2,3},{4,false,6,7,8},{10,"ab",{"aaa",11,true},13,14},{"aa","bb",17},{},{true,88}};
array y = x
.Select(function (array a) {; a;})
.Where(function (array a) {arrayCount(a) > 3;});
print(y); // {{4,False,6,7,8},{10,ab,{aaa,11,True},13,14}}</source><br/><source lang="javascript">array y = {20,30,40,50,70,80,90,100};
print(y.FirstOrDefault(function (number a) { true; })); // 20
print(y.FirstOrDefault(function (number a) { a > 70; })); // 80
print(y.First(function(number a) { true; })); // 20
print(y.First(function(number a) { a > 40; })); // 50</source>


<span style="color:#a52a2a;">'''Methods'''</span>  
<br/><source lang="javascript">array a = {1,2,3,4,5,6,7,8};
a.Randomize();
print(a); // {1,5,2,8,7,6,4,3}</source>


*<span style="color:#000000;">'''Empty Add(AnyType element)''' - Add a new element to the Array.</span>
*<span style="color:#000000;">'''number Frequency(number Number)''' - Counts the number of times a given Number object exists in the Array. Can only use if all the elements are of type number. Number is the number to search for.</span>
*<span style="color:#000000;">'''bool IsCharacterArray()''' - Check if array is an array of characters.</span>
*<span style="color:#000000;">'''bool IsNumericArray()''' - Check if array is an array of numbers.</span>
*<span style="color:#000000;">'''bool IsNumericArray()''' - Check if array is an array of integer numbers.</span>
*<span style="color:#000000;">'''bool IsStringArray()''' - Check if array is an array of string.</span>
*<span style="color:#000000;">'''Empty set_Item(number index, AnyType value)''' - Set element value at index.</span>
*<span style="color:#000000;">'''AnyType get_Item(number index)''' - Get element at index.</span>
*<span style="color:#000000;">'''Empty RemoveItemAt(number index)''' - Remove an element from the Array. index is the index to remove the object from.</span>
*<span style="color:#000000;">'''string ToString()''' - The string representation of the object.</span>
*<span style="color:#000000;">'''array Select(Function selector)''' - Projects each element of a sequence into a new form. selector is a transform function to apply to each element.</span>
*<span style="color:#000000;">'''array Where(Function predicate)''' - Filters a sequence of values based on a predicate. predicate is a function that takes 1 parameter of the types in the array and return true/false.</span>
*<span style="color:#000000;">'''array OrderBy(Function comparer)''' - Sorts the elements of a sequence in ascending order by using a specified comparer. comparer is a Function that compares two objects of the same type. Must return a signed integer that indicates the relative values of first param A and second param B. Value Less than 0&nbsp;: A is less than B.Value 0&nbsp;: A equals B.Value Greater than 0&nbsp;: A is greater than B.</span>
*<span style="color:#000000;">'''AnyType First(Function selector)''' - Returns the first element in a sequence that satisfies a specified condition. selector is a function to test each element for a condition.</span>
*<span style="color:#000000;">'''AnyType FirstOrDefault(Function selector)''' - Returns the first element in a sequence that satisfies a specified condition or empty if not found. selector is a function to test each element for a condition.</span>


<span style="color:#a52a2a;">'''Properties'''</span>  
<source lang="javascript">
//Example for using a.Except
array a = {1,2,3,4,5,6,7,8,9};
array b = a.Except({1,3,5,7,9});
print(a);//{1,2,3,4,5,6,7,8,9}
print(b);//{2,4,6,8}
</source>


*<span style="color:#000000;">'''number Average HasGetter'''&nbsp; - Average of the objects in the Array object. Can only use if all the elements are of type Number.</span>
<source lang="javascript">
*<span style="color:#000000;">'''number Count HasGetter'''&nbsp; - Number of elements in Array.</span>
//Example for using .Any and .Reverse()
*<span style="color:#000000;">'''number Max HasGetter'''&nbsp; - Largest of all the objects in the Array object. Can only use if all the elements are of type Number.</span>
  object e;
*<span style="color:#000000;">'''number Min HasGetter'''&nbsp; - Smallest of all the objects in the Array object. Can only use if all the elements are of type Number.</span>
  array lastPersonsUsed = empty;
*<span style="color:#000000;">'''string ObjectTypeName HasGetter'''&nbsp; - The name of the type of object.</span>
  try {
*<span style="color:#000000;">'''number Sum HasGetter'''&nbsp; - Sum of all the objects in the Array object. Can only use if all the elements are of type Number.</span>
    UserPreference pref = new UserPreference("Task_Add", 1);
*<span style="color:#000000;">'''TypeInformation TypeInformation HasGetter'''&nbsp; - Get information about this class.</span>
    lastPersonsUsed = Catglobe.Json.Parse(pref.Value);
  } catch (e) {  }
  if (lastPersonsUsed == empty)
    lastPersonsUsed = {};
  lastPersonsUsed = lastPersonsUsed.Reverse();
  lastPersonsUsed.Add({123, "Username"});
  array fixedLastPersonsUsed;


&nbsp;  
  lastPersonsUsed = lastPersonsUsed.Reverse();
  number cur = getCurrentUserUniqueId();
  for (i for 0; lastPersonsUsed.Count) {
      number userResourceId = lastPersonsUsed[i][0];
      if (cur != userResourceId && userResourceId != 0 && !fixedLastPersonsUsed.Any(function(array a){return userResourceId == a[0];}))
        fixedLastPersonsUsed.Add(lastPersonsUsed[i]);
  }
  new UserPreference("Task_Add", 1, Catglobe.Json.Encode(fixedLastPersonsUsed.Take(5)));


<span style="color:#a52a2a;">'''Examples'''</span>
</source>
 
&nbsp;
 
array x = new Array();
 
print(x);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&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:#008000;">&nbsp;//&nbsp;&nbsp; {}</span>
 
&nbsp;
 
array x = new Array(10);
 
print(x);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&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:#008000;">//&nbsp;&nbsp; {Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty}</span>
 
&nbsp;
 
array x = {10,20,30,40,50,60,70,80,90,100};
 
array y = x
 
.Select(function (number a) { a;})
 
<span style="font-size:10px;"><span style="color: rgb(0, 128, 0);">// .Select(function (string a) { a;})&nbsp; -&gt; will not work: Cannot assign a value of type 'Number' to a variable of type 'String'</span></span>
 
<span style="font-size:10px;"><span style="color: rgb(0, 128, 0);">// .Select(function (array a) { a;})&nbsp; -&gt; will not work: Cannot assign a value of type 'Number' to a variable of type 'Array'</span></span>
 
<span style="font-size:10px;"><span style="color: rgb(0, 128, 0);">// .Select(function (bool a) { a;})&nbsp; -&gt; will not work: Cannot assign a value of type 'Number' to a variable of type 'Boolean'</span></span>
 
<span style="font-size:10px;"><span style="color: rgb(0, 128, 0);">// .Select(function () { a;})&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt; will not work: Function called with too many parameters @</span></span>
 
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i &gt; 40;});
 
print(y);&nbsp;&nbsp;
 
&nbsp;
 
array x = {10,20,30,40,50,60,70,80,90,100};
 
array y = x
 
.Select(function (number a) { 88888;})
 
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i &gt; 40;});
 
print(y);&nbsp;&nbsp;
 
&nbsp;
 
array x = {10,20,30,40,50,60,70,80,90,100};
 
array y = x
 
.Select(function (number a) { a;})
 
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i &gt; 40;})
 
.OrderBy(function(number a, number b) {if (a&gt;b) 1; else if (a&lt;b) -1; else 0;});
 
<span style="color:#008000;"><span style="font-size: 10px;">// .OrderBy(function(number a, number b){if (a&gt;b) -1; else if (a&lt;b) 1; else 0;});</span></span>
 
print(y);&nbsp;
 
&nbsp;
 
array x = {{1,2,3},{4,false,6,7,8},{10,&quot;ab&quot;,{&quot;aaa&quot;,11,true},13,14},{&quot;aa&quot;,&quot;bb&quot;,17},{},{true,88}};
 
array y = x
 
.Select(function (array a) {; a;})
 
.Where(function (array a) {arrayCount(a) &gt; 3;});
 
print(y); &nbsp;&nbsp;&nbsp;&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:#008000;">&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; {{4,False,6,7,8},{10,ab,{aaa,11,True},13,14}}</span>
 
&nbsp;
 
array y = {20,30,40,50,70,80,90,100};
 
print(y.FirstOrDefault(function (number a) { true; }));&nbsp;&nbsp; &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:#008000;">// 20</span>
 
print(y.FirstOrDefault(function (number a) { a &gt; 70; }));&nbsp;&nbsp; &nbsp;&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:#008000;">// 80</span>
 
print(y.First(function(number a) { true; }));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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:#008000;">// 20</span>
 
print(y.First(function(number a) { a &gt; 40; }));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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:#008000;">// 50</span>  
 
&nbsp;
 
&nbsp;
 
&nbsp;
 
[[Category:Data_Types_Literals_and_Variables]]

Latest revision as of 09:40, 25 April 2022

Array



The array object

Parent class

Inherits from object

Constructors

  • () - Creates an empty array
  • (array toCopy "Array to copy") - Creates a copy of another array
  • (int count "Number of items to put in the array") - Creates an array with a predefined size, where all elements are Empty

Methods

  • Empty Add(object element "element to add") - Add a new element to the Array
  • Empty AddRange(array source "Array to get elements from.") - Add all elements of another array to this array.
  • bool Any(Function predicate "A function that takes 1 parameter of the types in the array, and return true/false") - Return true if the array contains the element using the given function.
  • Empty Clear() - Remove all items in array
  • bool Contains(object element "Element to check for") - Return true if the array contains the element using the normal equal operator.
  • array Except(array elements "Elements to remove") - Return all elements that does not exist in the other collection.
  • object First(Function selector "A function to test each element for a condition.") - Returns the first element in a sequence that satisfies a specified condition.
  • object FirstOrDefault(Function selector "A function to test each element for a condition.") - Returns the first element in a sequence that satisfies a specified condition or empty if not found.
  • Empty Foreach(Function function "A function that takes 1 parameter of the types in the array, and an optional 2nd parameter that is the index") - Filters a sequence of values based on a predicate.
  • int Frequency(number number "The number to search for") - Counts the number of times a given Number object exists in the Array. Can only use if all the elements are of type Number
  • object this[] { get; }(int index "Index") - Get element at index
  • int IndexOf(object element "Element to search for") - Return index of the given element using ==, or -1.
  • int IndexOfObject(object element "Element to search for") - Return index of the given element using references, or -1.
  • bool IsCharacterArray() - check if array is an array of characters
  • bool IsNumericArray() - check if array is an array of integer numbers
  • bool IsStringArray() - check if array is an array of string
  • array OrderBy(Function comparer "Function that compares two objects of the same type. Must return a signed integer that indicates the relative values of first param A and second param B. Value Less than 0 : A is less than B.Value 0 : A equals B.Value Greater than 0 : A is greater than B.It can also be a function that takes 1 parameter and returns a string or number.") - Sorts the elements of a sequence in ascending order by using a specified comparer.
  • Empty Randomize() - OBSOLETE! Randomize the order of the elements in the current array. Multiple calls to this may return the same randomization, use a Random object to preserve randomization over multiple calls
  • Empty Randomize(Random random "Random object") - Randomize the order of the elements in the current array.
  • Empty Remove(object item "Item will be removed") - Removes the first occurrence of an element from the Array
  • Empty RemoveItemAt(int index "The index to remove the object from") - Remove an element from the Array by its index
  • array Reverse() - Returns an array with all the elements in the opposite order.
  • array Select(Function selector "A transform function to apply to each element.") - Projects each element of a sequence into a new form.
  • array SelectMany(Function selector "A transform function to get each sub array.") - Projects each array element of a sequence into a new form.
  • Empty this[] { set; }(int index "Index", object value "Value to set") - Set element value at index
  • array Skip(int n "How many elements to skip") - Get all but the n first elements.
  • array Take(int n "How many elements to take") - Get the n first elements.
  • Dictionary ToDictionary(Function keySelector "A transform function to get the key of each element.") - Return a dictionary with the elements of the array.
  • Dictionary ToDictionary(Function keySelector "A transform function to get the key of each element.", Function valueSelector "A transform function to get the value of each element.") - Return a dictionary with the elements of the array.
  • string ToString() - The string representation of the object.
  • array Where(Function predicate "A function that takes 1 parameter of the types in the array, and an optional 2nd parameter that is the index and return true/false") - Filters a sequence of values based on a predicate.

Properties

  • number Average { get; } - Average of the objects in the Array object. Can only use if all the elements are of type Number
  • int Count { get; } - Number of elements in Array
  • number Max { get; } - Largest of all the objects in the Array object. Can only use if all the elements are of type Number
  • number Min { get; } - Smallest of all the objects in the Array object. Can only use if all the elements are of type Number
  • string ObjectTypeName { get; } - The name of the type of object.
  • number Sum { get; } - Sum of all the objects in the Array object. Can only use if all the elements are of type Number
  • (From object) TypeInformation TypeInformation { get; } - Get information about this class.


Examples

array x = new Array();
print(x); // {}


array x = new Array(10);
print(x); // {Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty}


array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { a;})
// .Select(function (string a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'String'
// .Select(function (array a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'Array'
// .Select(function (bool a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'Boolean'
// .Select(function () { a;}) -> will not work: Function called with too many parameters @
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;});
print(y);


array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { 88888;})
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;});
print(y);


array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { a;})
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;})
.OrderBy(function(number a, number b) {if (a>b) 1; else if (a<b) -1; else 0;});
// .OrderBy(function(number a, number b){if (a>b) -1; else if (a<b) 1; else 0;});
print(y);


array x = {{1,2,3},{4,false,6,7,8},{10,"ab",{"aaa",11,true},13,14},{"aa","bb",17},{},{true,88}};
array y = x
.Select(function (array a) {; a;})
.Where(function (array a) {arrayCount(a) > 3;});
print(y); // {{4,False,6,7,8},{10,ab,{aaa,11,True},13,14}}


array y = {20,30,40,50,70,80,90,100};
print(y.FirstOrDefault(function (number a) { true; })); // 20
print(y.FirstOrDefault(function (number a) { a > 70; })); // 80
print(y.First(function(number a) { true; })); // 20
print(y.First(function(number a) { a > 40; })); // 50


array a = {1,2,3,4,5,6,7,8};
a.Randomize();
print(a); // {1,5,2,8,7,6,4,3}


//Example for using a.Except
array a = {1,2,3,4,5,6,7,8,9};
array b = a.Except({1,3,5,7,9});
print(a);//{1,2,3,4,5,6,7,8,9}
print(b);//{2,4,6,8}
//Example for using .Any and .Reverse()
   object e;
   array lastPersonsUsed = empty;
   try { 
     UserPreference pref = new UserPreference("Task_Add", 1);
     lastPersonsUsed = Catglobe.Json.Parse(pref.Value);
   } catch (e) {  }
   if (lastPersonsUsed == empty)
     lastPersonsUsed = {};
   lastPersonsUsed = lastPersonsUsed.Reverse();
   lastPersonsUsed.Add({123, "Username"});
   array fixedLastPersonsUsed;

   lastPersonsUsed = lastPersonsUsed.Reverse();
   number cur = getCurrentUserUniqueId();
   for (i for 0; lastPersonsUsed.Count) {
      number userResourceId = lastPersonsUsed[i][0];
      if (cur != userResourceId && userResourceId != 0 && !fixedLastPersonsUsed.Any(function(array a){return userResourceId == a[0];}))
         fixedLastPersonsUsed.Add(lastPersonsUsed[i]);
   }
   new UserPreference("Task_Add", 1, Catglobe.Json.Encode(fixedLastPersonsUsed.Take(5)));