Saturday, August 20, 2011

Initialize (clear) an array

Initialize (clear) an array.

Solution

my @array = ();
 
 
Solution
$#array is the subscript of the last element of the array (which is one less than the length of the array, since arrays start from zero). Assigning to $#array changes the length of the array @array, hence you can destroy (or clear) all values of the array between the last element and the newly assigned position. By assigning -1, everything is destroyed and the array is cleared. I recommend the above solution to this one.

$#array = -1;

No comments: