Arrays

Accessing a group of elements from an array

Slice = a list (group) of elements from an array.

Examples:

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

@a = @measurement[2,3,4,5];     # @a is now (5, 9, 7, 13)
@a = @measurement[2..5];        # the same

@b = @measurement[0, 1, 3..6]; # @b contains all @measurement elements but the third

The .. denote a range.

Note that since a slice contains a list of elements it uses an @ prefix rather than a $.

Assignment of a list into an array slice

In the previous example we have assigned an array slice into an array variable.

Now let us do the opposite, and assign a list (or an array variable) into a slice.

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

@measurement[2..4] = (6, 10, 8);

The values of @measurement:

Before

3

2

5

9

7

13

16

After

3

2

6

10

8

13

16

Assignment of an array (or list) into another array

Examples:
@a = (1, 2, 3);
@b = @a;            #now @b contains (1, 2, 3)

@a = (1, 2, 3);
@b = (0, @a, 4, 5); #now @b contains (0, 1, 2, 3, 4, 5)

Note that the variable @a is first decomposed to the list of the values it contains, and then these values are inserted into @b.


Table of Contents.
Previous | Next.