Skip to content

Array

The array data-type is for storing an ordered list of values. Each item in an array may be a different data type to other values in the array. However, once a value is inserted into an array, the normal rules apply about not being able to change it to another data-type.

The key thing that differentiates an array from an object, is the fact that an array's internal values are identified by numbers instead of names. For example, this is how we might retrieve a value from an array:

1
var result = myArray[4];

In this example, our array variable is called myArray and we are retrieving its 4th element, which we intend to store in a new variable called result


An array is a complex data-type which means that Modulo1 will avoid making copies of this kind of variable, as much as possible. And this behavior is quite common in modern programming languages, but it can be a source of confusion for programmers. So, if this concept sounds foreign to you, you need to pay close attention right now.

When a simple variable is used as the input to a function, Modulo1 makes a copy of that variable and sends the copy to become the input of the function. This means that if the function makes changes to the variable, it will not affect the original variable: the changes will only occur to the copy of the variable. For example:

1
2
3
4
5
6
7
var myAge = 32;

function double(number age) {
    age *= 2;
}
double(myAge);
print(myAge);

In this example, we use the variable myAge as the input to the function double(). Modulo1 creates a copy of myAge and then allows double()** to refer to this copy as age. So, when double() performs calculations upon age, our original variable myAge is not affected. When we print myAge, we will see: 32.

On the other hand, when a complex variable is used as the input to a function, Modulo1 does not make a copy. Instead, Modulo1 sends the original version to the function. This means that if the function makes changes to the variable, it will affect the original variable. For example:

1
2
3
4
5
6
7
var dreamCars = ["BMW", "Ferrari", "Porsche"];

function addHonda(array cars) {
    cars.addToEnd("Honda");
}
addHonda(dreamCars);
print(dreamCars);

In this example, we use the array dreamCars as the input to the function addHonda(). Modulo1 allows addHonda() to refer to the original version of dreamCars as cars. So, when addHonda() adds a new value to the end of cars, or original variable dreamCars is affected. When we print dreamCars, we will see: ["BMW", "Ferrari", "Porsche", "Honda"]

This behavior also occurs when you save an array to a new location, such as into another array, into an object, or just under a new variable name. Simple variables will be duplicated, when saved to a new location, but complex variables will not. A consequence of this behavior, is that a complex variable can exist in an unlimited amount of locations at any one time. For example:

1
2
3
4
5
6
7
8
9
var colors = ["Red"];
var colors2 = colors;
colors2.addToEnd("Blue");

var things = { one : colors2 };
things["one"].addToEnd("green");

colors2.addToStart("Orange");
print(colors);

In this example, we assigned our original array colors to many locations. We assigned it to colors2. Then we added "Blue" to colors2. Next, we assigned colors2 to the one element of things. Finally, we added "Orange" to the start of colors2. If colors had been a simple variable, such as a string, then it would never have been changed in this example. A copy would have been made each time it was assigned to a new location and at each location the changes there would not affect any other locations. But instead, because Modulo1 recognises colors as an array, it means that its value was never copied. The exact same copy of the exact same value is being held by several variables. So, when one of these variables is changed, all of them are changed. When we print colors, we will see: ["Orange", "Red", "Blue", "green"]


Syntax

To create a new array:

1
var dreamCars = ["BMW", "Ferrari", "Toyota", "Mazda", "Honda"];

Spaces and line breaks are not important. The following achieves the exact same result:

1
2
3
4
5
6
7
var dreamCars = [
    "BMW",
    "Ferrari",
    "Toyota",
    "Mazda",
    "Honda"
];

To create a truly separate copy of an array:

1
var copyOfCars = array(dreamCars);

To delete an array:

1
delete copyOfCars;

To delete an element that is within an array:

1
delete dreamCars[3];

To access an individual item from within an array:

1
var firstCar = dreamCars[0];

Like most programming languages, in Modulo1 we start counting from zero, which means the first item in this array ("BMW") is at position 0, and therefore we access it via dreamCars[0]. The second item ("Ferrari") is at position 1, so we access it via dreamCars[1]. The last item in this array, (Honda) is at position 4, so we access it via dreamCars[4].

To change an individual item within an array:

1
var dreamCars[0] = "Mercedes";

To create a new array filled with duplicates of the same value:

1
var severalSevens = 10 of 7;

The above example, creates an array called severalSevens that has 10 instances of the number 7

To loop through all the elements in an array:

1
2
3
for (var car in dreamCars) {
    print(car);
}

In this example, we ask Modulo1 to create a temporary variable called car which is of the iterator data-type. An iterator is a special kind of variable that does not hold any data of its own. Instead it points to other variables, which is especially useful when running loops. On each cycle of our loop, the iterator will point to a different element within our dreamCars object. This allows us to treat the iterator car as if it actually was that particular element of the dreamCars object. Anything we do to car, within this for loop, will actually be done to every element of dreamCars.

To loop through some of the elements in an array:

1
2
3
for (var i in 2 to 4) {
    print(dreamCars[i]);
}

Properties

A property is a special feature of a variable that can be used to get and/or set some aspect of that variable.

length

The length property can be used to find out how many values are stored within a particular array variable.

1
var result = dreamCars.length;

The length property can also be used to change the length of an array.

1
dreamCars.length = 10;

type

The type property can be used to find out the data-type of a particular array variable. The result will always be "array"

1
var result = dreamCars.type;

Functions

Info

The following feature set is a work-in-progress. Come back later for updates

reverse()

The reverse function reverses the order of the items in an array

1
dreamCars.reverse();

reverse( array x )

The reverse function reverses the order of the items in an array

1
array.reverse(dreamCars);

addToEnd( null x )
addToEnd( number x )
addToEnd( boolean x )
addToEnd( array x )
addToEnd( object x )
addToEnd( iterator x )
addToEnd( melody x )
addToEnd( drumRiff x )
addToEnd( function x )
addToEnd( class_object x )

The addToEnd function inserts the variable x at the end of the array

1
dreamCars.addToEnd("McLaren");

addToStart( null x )
addToStart( number x )
addToStart( boolean x )
addToStart( array x )
addToStart( object x )
addToStart( iterator x )
addToStart( melody x )
addToStart( drumRiff x )
addToStart( function x )
addToStart( class_object x )

The addToEnd function inserts the variable x at the start of the array

1
dreamCars.addToEnd("McLaren");

joinAtEnd( array x )

The joinAtEnd function inserts the elements of array x at the end of the current array

1
dreamCars.addToEnd(["McLaren", "Lamborghini"]);

joinAtStart( array x )

The joinAtEnd function inserts the elements of array x at the start of the current array

1
dreamCars.joinAtStart(["McLaren", "Lamborghini"]);

removeFromEnd()

The removeFromEnd function removes one element from the end of the current array and returns it

1
var result = dreamCars.removeFromEnd();

removeFromStart()

The removeFromStart function removes one element from the start of the current array and returns it

1
var result = dreamCars.removeFromStart();