All Tutorials / Steve G / Explanation and Uses of Arrays.

Explanation and Uses of Arrays.

Step 5. Variable Arrays.

Before we go over variable arrays, we'll quickly go over what a variable is.

The easiest way to describe a variable, is to compare it to a container. A container stores an item. You can open the container and change the item, but it will only hold one item. A variable is a memory location that stores a data value. This data value can change, but the variable can still only contain one data value.

So where does an array come in to it? As we have seen above, a single variable is a memory location, but putting a group or series of these memory locations together will create an array. Let's look back at part of our ultrasonic sensor script again...

Code:

$ping1 = GetPing(d1, d2)
$ping2 = GetPing(d3, d4)
$ping3 = GetPing(d5, d6)
$ping4 = GetPing(d7, d8)
$ping5 = GetPing(d9, d10)



Each "GetPing()" line is the memory location for each variable, and each one has their own unique name "Ping1, 2, 3, 4, and 5".

Code:

if ($ping1 < 30)
Goto(Detection)
endif

if ($ping2 < 30)
Goto(Detection)
endif

if ($ping3 < 30)
Goto(Detection)
endif

if ($ping4 < 30)
Goto(Detection)
endif

if ($ping5 < 30)
Goto(Detection)
endif



We now have the memory locations put together, which now makes our variable array.

When the script is run, the value of each variable is can also be displayed, including the values within arrays, in the variable watcher control seen below...

User-inserted image