in reply to How do I completely empty out an array?

Many different ways, often differing in how much memory perl holds on to. I'm going to try not to be too technical, so I will probably end up not pedantically correct.

  • undef(@array) frees all the memory associated with the array except the bare minimum, as if @array had been declared but never used.
  • splice(@array), $#array = -1, and @array = () all empty the array, but hold on to all the indices in case they are used again. There may be slight technical differences between some of these.
  • for (@array) { undef $_ } (or the equivalent C-like:
    for ($i = 0; $i<=$#array; ++$i) { undef $array[$i]; }
    leaves the length of the array alone (so scalar(@array) and $#array are unchanged) but blows away all the contents, including any string buffers they might have used.
  • for (@array) { $_ = undef } (or the equivalent C-like:
    for ($i = 0; $i<=$#array; ++$i) { $array[$i] = undef; }
    also leaves the length of the array alone (so scalar(@array) and $#array are unchanged) but undefines all the contents. However, if $array[42] was "what is the meaning of life", $array[42] will still have a 28+ byte area reserved for a string so assigning $array[42]="python's flying circus" will not require perl to allocate any new memory.