4.1.1 Arrays

An array is a variable type that collects and stores several values under a single name and allows access through an index number.

Arrays are defined as var or global, like any other variable. Array definitions and access formats are as follows.

Definition

var array name = [ Value, Value, …]

Access

Array name [Index]

The values that make up an array are called “elements.” Distances, an array shown in the following example, has a total of five elements. The index starts from 0. Element 0 and e lement 1 of “distances” are 10 and 10.5, respectively.

The [ ] operator is used as follows to read or write the value of an array’s specific element value. The following shows an example of an object that is defined and accessed.

0001.job

var distances = [ 10, 10.5, 12.7, 11.92, 9.5 ]

distances[1]=20.5

print distances[0], distances[1]

end

Result

10

20.5

The number of elements in an array can be acquired by using the len() function. Previously, the len() function was introduced as a function to acquire the length of a string. If an array is put as a parameter of len( ), it will return the number of elements in the array.

Function name

Description

Example of usage

Result

len(a)

Returns the length of the string if a is a string. Returns the number of elements in the array if a is an array

len("HELLO")

len([20, 30, 80])

5

3

The for-next statement is mainly used to perform some processing on all elements of an array.

0001.job

var i

var distances = [ 10, 10.5, 12.7, 11.92, 9.5]

for i=0 to len(distances)-1

distances[i] = distances[i]+10

print distances[i]

next

end

Result

20

20.5

22.7

21.92

19.5

It does not matter if the values stored in the array are of different types.

0001.job

var i

var arr = [ 10, "abc", true]

for i=0 to 2

print arr[i]

next

end

Result

10

abc

true

Last updated

Was this helpful?