Arrays

Top  Previous  Next

Arrays are ordered collections of data of one type. Each data item is called an element, and is accessed by its 

position (index) in the array. They are very useful for storing lists of data, such as customers, or lines of text. 

There are a number of types of array, and array may be single or multidimensional (lists of lists in effect). 

 

Constant arrays:

 

It is probably easiest to introduce arrays that are used to hold fixed, unchangeable information. 

These can be defined in two kinds of ways:

 

 

 

 

Defining an array size using enumerations:

 

 

Below, we define an enumeration, then a subrange of this enumeration, and define two arrays using these.

 

 

 

 

 

Array operations

 

Array operations were added to the Smart Pascal syntax to better adapt to JavaScript. Since JavaScript has no concept of pointers or direct memory access, all attempts at dealing with lists, linked lists etc. in the traditional way would cause a massive speed penalty. So where you under Delphi or Free Pascal would allocate a TList or TObjectList instance - Smart Pascal achieves better performance and identical functionality using ordinary arrays. All arrays support a complete set of operations, regardless of datatype, for inserting, removing, sorting and otherwise manipulate the content.

 

Smart Pascal have this functionality "built-in" for all arrays, as long as the datatypes match. When you combine this with property expressions, the result is a powerful and alternative way of achieving the same with less resources and more speed than generics or traditional Object Pascal.

 

In addition to Low, High, Length and Count, Pas2JS also support the following pseudo-methods: 

 

·Add(item [,...]) / Push(item [,...]) : increases Length by one and adds one or more item at the end of the array, can also add arrays (concatenation).
·Clear() : empties the array (equivalent to SetLength(0))
·Copy(startIndex[, count]) : creates a new dynamic array containing count items from startIndex, if count isn't specified, copies to the end of the array.
·Delete(index[, count]) : deletes the item at the specified index and reduces Length by count (default one).
·IndexOf([startIndex, ]item) : returns the index of an item, returns a negative value if not found.
·Insert(index, item) : insert an item at the specified index.
·Join(delimiter : String) : joins the elements of an array into a string, and returns the string.
·Map([map function]) : creates a new array by mapping elements of the array according to a map function. The map function takes a single parameter of the type of the array items and returns the mapped value.
·Peek() : returns the last item.
·Pop() : returns the last item and removes it from the array.
·Remove([startIndex, ]item) : removes the item if it can be found and returns its previous index, returns a negative value if not found.
·Reverse() : reverses the order of the items.
·SetLength(newLength) : defines the length of a dynamic array
·Sort([compare function]) : sort an array according to the compare function, the comparison function can be omitted for arrays of String, Integer and Float.
·Swap(index1, index2) : swaps to items of specified indexes.

 

 

The Peek(), Pop(), Push() method are typically used with FILO (first in, last out) and FIFO (first in, first out) stack objects. As such Smart Pascal has no need for classes like TObjectlist, TList, TStringlist and TStack (these are provided by the run-time library for legacy support with Delphi's Visual Component Library only).

 

 

Using Arrays with StrSplit and StrJoin functions in Pas2JS

 

·StrJoin() takes an array of string and returns a string made by joining all the individual strings

 

·StrSplit() splits a string on a delimiter and returns an array of string

 

------------------------------------------------------------------------------------------------------------------------

 

See more at Array object JS reference.

 

 

 

Array Iterations using for..in

 

 

Pas2JS supports the statement for in do to loop on elements of an array in a sequence, for instance.

 

Examples using for in do in Pas2JS

 

 

 

Array Types:

 

 

Arrays come in two varieties: static arrays where the length of the array is known during the compilation 

and dynamic arrays which can be resized during the program execution. 

 

 

 

Static Arrays 

Static Arrays require the size to be defined as part of the array definition. They are called static because their size is static, and because they use static memory.
 

 Defining static arrays in Pas2JS

 

 

 

How do I create a multidimensional array in Pas2JS?

 

 

 Multidimensional array 11x11 in Pas2JS Model 1 - using new

 

 Multidimensional array 11x11 in Pas2JS Model 2 - using SetLength  

 

 Multidimensional array 11x11 in Pas2JS Model 3 - using auxiliar array

 

 Bi-dimensional array in Pas2JS

 

 

 

Dynamic Arrays 

Dynamic arrays are supported as reference types. They are declared without bounds. Their lower bound is always zero. 

 

Dynamic arrays can be initialized from inline or constant static arrays.

 

 Initialize dynamic arrays in Pas2JS

 

 

Declaring Arrays

 

First thing you have to do is define an array and then create it.

 

Create an object Array

 

Next step is create an object array (you instantiate with the keyword new).

Pas2JS supports dynamic arrays in the form of objects (which in reality maps directly to JavaScript arrays). You allocate dynamic arrays using the classical "new" keyword, as such:

 Declaring and instantiate an array in Pas2JS example 1

 

 Declaring and instantiate an array in Pas2JS example 2

 

 

 

Passing Dynamic Arrays by Reference in Pas2JS

 

 Passing Dynamic Arrays by Reference in Pas2JS

 

 

Using the prefix var to return a different dynamic array

The var keyword still makes sense if you want to return a different array.

 Using the prefix var to return a different dynamic array

 

 Passing array as reference in  Pas2JS

 

 

 

Single Dimensional arrays in Pas2JS

 

So far, we have only seen lists - single dimensional arrays.

 Set the capacity of a array to 3 elements in Pas2JS

 

 

 

Multidimensional arrays in Pas2JS

 

Pas2JS supports arrays of any numbers of dimensions. In reality, a multidimensional array is a collection of arrays - each element of the first array is another array. each element of that array is in turn another array and so on.

 

Dynamic arrays do not have their size defined in their declaration.

 

 

Multidimensional Arrays

 

In Delphi, we could have defined the array 4 rows per 5 colluns with one SetLength statement:

SetLength(multiArray, 45); 

 

In Pas2JS, we have to define the size of our multidimensional array in pieces (sized subarrays). 

Take a look at this code:

 

 Array 4 x 5 in Pas2JS

 

 

Copying an Array

 

 

When copying single dimension arrays, we can use the Copy(startIndex : Integer; count: Integer) method. It allows us to copy all or part of one array to another, as in this example:

 

 Copy part of one Array in Pas2JS

 

 

 

Array of Records

 

 

Let's step into another dimension and create an array of records which allows you to store multiple records which can be returned to a calling form. Place the following declaration into the interface section of a form.

 

 Arrays of Records example in Pas2JS example 1

 

 Arrays of Records example in Pas2JS example 2

 

 

 

New X Copy()

 

Dynamic arrays in Pas2JS are pure reference types and they behave a bit like a TList<T> would in Delphi, as SetLength() is a method, which modifies the array, rather than a global procedure, which can create a new copy of the array  (as in Delphi), ie. in Pas2JS, if you have: 

 

 Dynamic array instance using New in Pas2JS

 

 

 

Copy method

 

 

The Copy() method creates a new dynamic array containing count items from startIndex, if count isn't specified, copies to the end of the array.

·Copy() copies the whole array.
·Copy(index) copies all items starting from index.
·Copy(index, count) copies count items starting from index.

 

 Using Copy to create a new dynamic array in Pas2JS

 

 

 

 

IndexOf method

 

The indexOf() method searches the array for the specified item, and returns its position. 

The search will start at the specified position, or at the beginning if no start position is specified, 

and end the search at the end of the array.

Returns -1 if the item is not found.

If the item is present more than once, the indexOf method returns the position of the first occurence.

Note: The first item has position 0, the second item has position 1, and so on.

 

 Using IndexOf in Pas2JS

 

 

Pop method

The pop() method removes the last element of an array, and returns that element. Note: This method changes the length of an array. Tip: To just return the last item, use the peek() method.

 Using Pop in Pas2JS

 

 

Push method

The push() method adds new items to the end of an array, and returns the new length.

Note: The new item(s) will be added at the end of the array.

Note: This method changes the length of the array.

 

 Using Push in Pas2JS

 

 

Insert method

The Insert() method insert an item at the specified index.

 Using Insert to add one item at specific index to an array in Pas2JS

 

Peek method

The Peek() method returns the last item.

 Using Peek in Pas2JS

 

Swap method

The swap() method swaps to items of specified indexes. 

 Using Swap in Pas2JS

 

Reverse method

The Reverse() method reverses the order of the items.

 Using Reverse in Pas2JS

 

 

Join method

The Join() method joins the elements of an array into a string, and returns the string.

 Using Join in Pas2JS

 

Delete method

The Delete() method deletes the item at the specified index and reduces Length by count (default one). 

 Using Delete to erase an item in Pas2JS

 

Sort method

The sort order can be either alphabetic or numeric, and either ascending or descending.Default sort order is alphabetic and ascending.  

 Using Sort in arrays in ascending/descending order in Pas2JS

 

Map method

The Map method creates a new array by mapping elements of the array according to a map function. The map function takes a single parameter of the type of the array items and returns the mapped value. 

The following code takes an array of numbers and creates a new array containing the square roots / squares of the numbers in the first array.

 

 Using Map in Pas2JS