Skip to content

Handling Data

Creating new data

Data stored in memory, is referred to as a variable. To create a new variable, we must do two things:

  1. Specify what name will be used to identify the variable
  2. Specify what the value will be of that variable

For example, to store a person's age as a variable we would write the following instruction:

1
var age = 34;

On the left side of the equals (=) sign, we write the keyword var, followed by the name of the variable (age). On the right side of the equals sign, we write the value (34), followed by a semicolon (;). Most of the instructions in the Modulo1 language require a semicolon at the end.

Info

For more information, check out our page on variables


Changing the value of existing data

The syntax for changing the value of a variable is similar to the syntax of creating a variable. For example, to set our age variable to 34 and then change it to 25, we would write the following instructions:

1
2
var age = 34;
age = 25;

The difference, is that when we change the variable, we do not include the keyword var.

An important concept to be aware of however, is that Modulo1 has automatically detected that the age variable is holding a number. And therefore, if you try to change the value of this variable, Modulo1 will expect you to change this variable to another number. If, instead, you were to try to change this variable to something other than a number, then the Modulo1 would consider this to be an error and attempt to shut down the code.

For example, the following sequence of instructions would result in an error.

1
2
var myNumber = 1056;
myNumber = "this line will cause an error";

Info

For more information, check out our page on variables


Changing the type of existing data

Usually, Modulo1 does not allow you to change a variable of one type into a different type. The reason for this, is that Modulo1 assumes that this was a mistake and the belief is that it is better to alert you to that mistake than for the program to run incorrectly.

However, there might be situations where you want to change the variable's data-type, without Modulo1 identifying this instruction as an error. There are a few ways to achieve this.

The first, is that you can simply delete the variable and then re-use its name to create a new variable:

1
2
3
var myNumber = 1056;
delete myNumber;
myNumber = "this is totally ok, now!";

The second, is that you can change the variable to null, and then change it to any value:

1
2
3
var myNumber = 1056;
myNumber = null;
myNumber = "this is also ok!";

The final way, is to use the special command "convert":

1
2
var myNumber = 1056;
convert (myNumber) to (string);

The types of variables available in Modulo1 are: null, number, boolean, string, array, object, iterator, melody, drumRiff, function, and class_object

Info

For more information, check out our page on variables


Retrieving code or data

It is highly encouraged to re-use and modify code or data that has been published by other users.

To find a drum:

1
var kick = find drum in (catalogue) where (id == "xUYRHGDJBGR");

To find an instrument:

1
var bass = find instrument in (catalogue) where (id == "HJKFJBA");

To find a drumRiff:

1
var riff1 = find drumRiff in (catalogue) where (id == "HKHKDHK");

To find a melody:

1
var bassline = find melody in (catalogue) where (id == "HKHJKFB");

Info

The catalogue is a work in progress and some aspects of it may not currently be available


Performing mathematical calculations

Quite often, we need to store the result of a calculation. For example:

1
var width = (275 * (55 + 7) / 3 - 12) % 5;

If you have not programmed before, some of the symbols in this line of code might be unfamiliar to you. If so, the following is a quick guide: * The * symbol is used for multiplication * The / symbol is used for division * The % symbol is used for modulus

Info

For more information on mathematical calculations, check out our page on expressions
For information on more advanced calculations, check out our page on numbers