In the context of "How do I completely empty out an array", your solution,

But this doesn't change the size of @array. To do this, you can use $#array = 0;

...is incorrect advice.

Per the Camel book: "Assigning to $#array changes the length of the array." So in that respect you are accurate. However, $#array is the subscript of the last element of @array. Zero is the first element of any array, unless you've messed with $[ (which you shouldn't have). So assigning zero to $#array will turn it into a one-element array, it won't erase that last element, $array[0].

Also per the Camel book: "You can truncate an array down to nothing by assigning the null list to it. The following two examples are equivalent: @whatever = (); or $#whatever = -1;"

So to reiterate, the following examples will both empty out an array:

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

It's up to you to decide which is easier to read.

Regarding the other suggestion of @array = map { undef } @array;, that will work if your intention is to undefine the value of each element of the array, but leave the array as large as it was in the first place. If you intend to put stuff into the array again, and it's likely to grow to be as big as it was before, leaving it large will offer a slight speed improvement (similar to pre-extending an array by saying, $#array = 999;, which creates a pre-extended but empty array of 1000 elements. Of course this speed improvement comes at the cost of the overhead required to hold onto 1000 empty elements.

But if your intent is to shrink the size of the array altogether, and not retain the undef elements, you should use the @array = (), or $#array = -1 construct.

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein


In reply to Re: Re: Answer: How do I completely empty out an array? by davido
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.