Arrays

Summary

Definition

An array variable contains an ordered list of scalar data.
It starts with an @ sign.
@k = (2, 4, 6..11, "dan", "gad", $a, $b);
@f = ();           #an empty array   

The foreach loop

foreach $i (@some_array) {
   ___________;
   ___________;
   ___________;
}

Binary assignment operators

$k += $n;     #same as $k = $k +$n;
$k -= $n;
$k *= $n;
$k /= $n;

$k .= $string;

$k++; $k--;   # increment / decrement $k by 1

Printing all array elements at once

print "Measurements: @msr \n";

Accessing individual array elements

$a = $msr[2];
$msr[7] = $b;

Array slices

@b = @msr [0, 1, 3..6];
@msr[2..4] = (6, 10, 8);

Assignment of an array (or list) into another array

@b = @a;
@b = (0, @a, 4, 5);

The sort function

@array2 = sort (@array1);   # sorts alphabetically
                            # @array1 remains unchanged

The push function

push (@array, $a);

# $a is added to the end of @array.

The shift function

$k = shift (@array);

# The first element of @array is removed and assigned to $k

Determining an array length

$n = @array;
$n = scalar (@array);
$n = $#array +1;


Table of Contents.
Previous