Arrays

Accessing individual array elements

Individual array elements may be accessed by indicating their position in the list (their index).

Example:

@measurement = (3, 2, 5, 9, 7, 13, 16);

index

value

0

3

1

2

2

5

3

9

4

7

5

13

6

16

First element: $measurement[0] (here has the value of 3),

Third element: $measurement[2] (here has the value of 5),

and so on.

Examples

Assignment of an individual array element into a scalar variable:

$second_elem = $measurement[1];

Assignment into an individual array element:

$measurement[5] = 24;
$measurement[7] = $value;
$measurement[$index] = $value;

#assuming that $index and $value have been assigned a value before


Table of Contents.
Previous | Next.