Variables - Arrays

Top  Previous  Next

Remember those tool shelves and drawers in garage ? They have function of arrays - you put similar tools on the same shelve or drawer. Screws of different size go to different drawers.

Array is also shipment of some stocks in the truck:

ArrayTruck

 

On this truck you can see how entire shipment contains similar items (as the label on truck doors suggests). Every item has it's own number.

That's a reason why one would want to use arrays in program - they help programmer to hold similar data in one container. It's easier to load all similar items in one truck, than requesting many trucks with different names.

ArrayFields

MMB uses arrays too. We call them 1-dimensional arrays, because items are stored in one "line" (dimension) - when your program puts or requests data from this kind of array, it uses only one coordinate (ordinal number of item) to go through array and perform operation on desired item.

 

Array in MMB is a group of variables under the same name, with addresses, so we can reach every item in array. In MMB script language, arrays are used just like variables.

 

Let's compare them...

 

Variable

Array

a$

a$[n]

name$

name$[n]

clicks

clicks[n]

user_address$

user_address$[n]

user_counter

user_counter[n]

 

On the left side are variables (string and numerical), and on the right are those same variables but in array form - extended form of variables, while we can store more items in arrays.

Let's take one array:

user_counter[n]

Here we have label of array ( user_counter ), two square brackets [ ] and item index ( n ). This is basic form of array. Array label can be specified just like in variable case. Square brackets enclose index number - either numerical variable or plain number that instructs MMB what item we want to retrieve from array.

If you've read section of manual about integer and string variables, you know they can be used almost anywhere in MMB. One of variable roles is to specify address of an item in array. These addresses are numbers, so we use numerical variables.

Here are some examples:

Arrays where item address is specified through ordinal numbers:

a[1]

twain$[72]

my_finger[534]

tool$[102]

 

Arrays where item address is specified through numerical variable:

a[i]

twain$[chapter_no]

my_finger[finger_number]

tool$[tool_drawer]

Arrays are frequently used in for...next loops, where variables are better solution for items addresses.

When you fill array, newest item gets highest address in array:

ArrayItemOrder

To specify item address you can even use math operations:

a[i + 1]

twain$[chapter_no - 4]

my_finger[finger_number * 8]

tool$[ tool_drawer / 2 ]

And how will you fill arrays ? Depends, what kind of array you're using:

 

Numerical arrays

Like numerical variables, MMB can use numerical arrays to store numbers. We use them for math operations. Both floating point and round numbers can be stored (83.21, 10, 294.1).

 

Let's fill some numerical arrays:

a[1] = c

my_finger[i] = 15

electricity[f + 1] = variable_volts

First example: array called " a[] " is filled with variable " c ", on item address 1.

 

Second example: array called " my_finger " is filled with number " 15 ", on item address received from variable " i " .

 

Third example: array called " electricity[] " is filled with variable " variable_volts ", on item address received from calculation: variable " f " + 1 .

Reading of items is similar:

c = a[1]

finger = my_finger[i]

power = electricity[f + 1]

First example: item on address " 1 ", from array " a[] ", is copied to numerical variable " c " .

 

Second example: item on address received from variable " i " , in array called " my_finger ", is copied to variable " finger " .

 

Third example: item on address calculated from variable " f " + 1, in array called "electricity[]", is copied to variable " power " .

 

String arrays

Like MMB string variable, this kind of array - string array - can store words, sentences, paragraphs of text and numbers. Basic difference in comparison with variables is possibility of storing many individual items.

String array looks like this:

a$[i]

It contains name of array ( a ), $ suffix that tells MMB we're using string array, square brackets [ ] and item address (numerical variable or ordinal number).

 

Filling string arrays looks like this:

a$[1] = c$

finger_names$[i] = name$

library$[f + 1] = book$

First example: array called " a$[] " is filled with content of variable " c$ ", on item address 1.

 

Second example: array called " finger_names$[] " is filled with content of variable " name$ ", on item address received from variable " i " .

 

Third example: array called " library$ " is filled with content of variable " book$ ", on item address received from calculation: numerical variable " f " + 1 .

 

Reading items from string arrays:

c$ = a$[1]

name$ = finger_names$[i]

book$ = library$[f + 1]

First example: item on address " 1 ", from array " a$[] ", is copied to string variable " c$ " .

 

Second example: item on address received from numerical variable " i " , in array called "finger_names$[]", is copied to string variable " name$ " .

 

Third example: item on address calculated from numerical variable " f " + 1, in array called " library$[] ", is copied to string variable " books$ " .

 

Examples of arrays in loop

As already mentioned, arrays are frequently used in loops. Here's one example:

for i=1 to 50

a[i] = i

next i

This code will fill 50 items of numerical array called " a[] " with numbers from 1 to 50. Address of item is received from for...next loop.

Reading values after filling array can be done using loop too:

for i=1 to 50

a = a [i]

Message("Array item contents: ","a")

Pause("200")

next i

Address of item is received from for...next loop. Every item is copied to numerical variable " a ", and then displayed in MMB's message box.

 

 

String arrays as object labels

 

 

Flexibility of MMB enables using content of string arrays as a source for object labels, used for various script commands:

StringArrayLabel

 

As an image above shows, HotSpot object labels Spot1, Spot2 and Spot3 have been assigned to string array label$[], having items label$[1], label$[2] and label$[3] .

 

Once labels are there, every script command that refers to some object label can use string array item instead of (fixed) label. So first you assign object label to string array item:

label$[1]= 'Spot1'

 

...and then execute script command that requires object label. For example, RunScript command uses one parameter - script label. Here we set string array item instead of fixed object label:

 

RunScript("label$[1]")

 

This feature is very useful when objects have uniformed labels (Text1, Text2, Text3...) and performed commands should refer to variable labels - usually in for..next loops :

for i=1 to 10

text$ = 'Hello no. '+CHAR(i)

label$[i]='Text'+CHAR(i)

LoadText("label$[i]","text$")

next i

 

For full understanding of MMB arrays it is highly recommended to read manual sections on variables and loop subjects.