What does assigning -1 to $#array do?

Viewed 83

Take this code:

$#mdCntrs = -1;

$#array returns the index of last element of array and returns -1 when array is empty.

But this assigning -1, what does that mean?

1 Answers

It changes the length of the array, in this case it will clear the entire array.

From perldoc perldata:

Assigning to $#days actually changes the length of the array. Shortening an array this way destroys intervening values.

It goes on to say:

The following are equivalent:

    @whatever = ();
    $#whatever = -1;
Related