Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does assigning -1 to $#array do?

Tags:

perl

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?

like image 565
Meenakshi Avatar asked Jan 26 '23 22:01

Meenakshi


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;
like image 73
Marcus Avatar answered Feb 03 '23 22:02

Marcus