Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I completely empty out an array?

( #2264=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 21, 2000 at 01:33 UTC
Q&A  > arrays


Answer: How can I completely empty out a list;
contributed by vroom

You can do any of these things: assign an empty list to the array, undef it or assign -1 to its "last index value".

@array=(); undef @array; $#array = -1;

$#array = -1 was originally contrbuted by Benne -- Ed.

Answer: How do I completely empty out an array?
contributed by ysth

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.
  • Answer: How do I completely empty out an array?
    contributed by DigitalKitty

    Hi.

    You could also use:

    splice( @array );
    Answer: How do I completely empty out an array?
    contributed by bradcathey

    And just in case you make the mistake I did, you can't use:

    @array = "";
    As the @array will be assigned a 'value' and will contain one element equal to "".

    Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2014-10-20 05:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (72 votes), past polls