3 - 29 3 - 29
MELSEC-Q
3 LET'S CREATE AND EXECUTE A PROGRAM
3.8 How to Use Arrays
Let's assume that a quantity of 100 data must be assigned to variables. Using A1, A2
... as variables, for example.
10 A1=52
20 A2=60
30 A3=17
………
We need 100 lines
of code.
100 variables will be required, and that's a lot of trouble. It is much more convenient to
use arrays in such instances.
It is necessary to tell BASIC that arrays will now be used. This is done by using the
DIM instruction.
DIM A(99)
This allocates locations to place data as shown below.
Since the number within the
parentheses is 99,
array entries 0 through 99
are allocated.
DIM A(99)
This allocates locations to place data as shown below.
A(0)
A(1)
A(2)
A(3)
A(98)
A(99)
………………
If 70 is to be assigned to A(50), enter the following.
A(50)=70
Now, the value within the parentheses can be specified by a variable, instead of a
number.
S=50 : A(S)=70
Since the value of S is 50, 70 will be assigned to A(50).
Now, let's consider assigning 100 data to variables again. When READ, DATA, and
FOR-NEXT from Section 3.5 and 3.6 are used.
10
DIM
A(99)
20
FOR
I=0 TO 99
30
READ A(I)
40
NEXT
50
DATA 52, 60, 70, • • •
How about this? The program is now amazingly short. Array variables become a useful
tool when repeating similar processes by specifying a variable for a value within
parentheses.