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.

    In reply to Re: How do I completely empty out an array? by ysth
    in thread How do I completely empty out an array? by vroom

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.