Creating an array
The definition of an array has 3 parts:- the type of the array
- one or more pairs of square brackets []
- the name of the array followed by a semicolon
Brackets must be immediately following the type and not the variable name as in C!
Initializing
In the declaration, you can optionally set the initial length of each dimension. For example, the following creates an array of type Strings with 5 empty indexes.Void fromJsonString(String json)
This function will append to the array the contents of the supplied JSON formatted string. Same functionality as fromXMLNode(XMLNode n), but it will parse the provided string directly without having to use parseJSON() yourself. Please note, this function will not clear the array, but rather append to it. Introduced or updated in version: 10.1.4Accessing indexed elements
Any element can be accessed by the variableName[pos]. Remember that the index starts at 0. The first element is at [0], the second at [1], and so on. The following code will set and then print the 1st element of the userPlans array.Length
You probably don’t go around remembering the size of every array. And as we shall see later, that number can actually change for dynamic arrays. It is also hard to maintain your script if you hard-code the length in multiple places. Thelength() function will return the number of indexable elements.
The last element will be at position length() - 1.
To print the length:
First and last
CRMScript has a set of functions for retrieving the 1st and last element in a collection. This notation is often shorter than using [pos].- first() returns the 1st element, same as [0]
- last() returns the last element, same as [length() -1]
- if the collection is empty, you will get an out-of-range exception
Inserting element at position x
We have seen that we can use an assignment statement to insert elements to a given index. You can also use theinsert() function, which adds an element at the given index.
Syntax: insert (index, elem)
Looping indexed collections
You can use both for and while loops to iterate the collection.Void sort()
This function will sort the array. It will only work on one-dimensional arrays. Arrays of basic types (Integer, String, Date, etc) will be sorted according to standard behavior. Arrays of complex types (SearchEngine, HTTP, etc) will not be sorted. Arrays of structs will be sorted if the struct has a function with the following signature: “Boolean compare(sameStruct other)”. Introduced or updated in version: 10.1.4Multi-dimensional collections
So far, we have focused on 1-dimensional collections. However, we can extend this by nesting: rather than inserting a number at index [0], we can insert an array of numbers. To create a 2-dimensional array, we need a double pair of square brackets in the declaration. The following example will build a 5 by 5 matrix of (x,y) coordinate pairs and then print it.Dynamic arrays
CRMScript supports collections that can grow and shrink dynamically. When you declare an array without initializing it, it is by definition dynamic. A fixed-size array can also become dynamic if you use any of the functions listed here.Pushing and popping
The dynamics come from being able to either push something into the collection, or pop something out of it. Think of the stack of plates at a restaurant buffet, often with a spring at the bottom. When the staff refills the stack, they push the new plates onto the top of the previous ones, and the bottom one gets lower and lower. When you grab a plate from the top, you pop it off, and the ones below rise upward.Adding elements
You can push elements to either the front or the back. If you specify the element, that’s what’s added. If you don’t specify anything, an empty element will be added and initialized to the correct type. Add to front:- pushFront()
- pushFront(elem)
pushBack() functions will append an element behind the current last element.
- pushBack()
- pushBack(elem)
Removing elements
You can pop elements from either the front or the back. The element which is popped is returned. Be sure to catch it if you intend to do something with it.- popFront() same as [0]
- popBack() same as [length() -1]
If the collection is empty, you will get an out-of-range exception
Deleting all elements
If you need to reset a collection, useclear() to remove all the elements. The length after this call will be 0.