Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I completely empty out an array?

by vroom (His Eminence)
on Jan 21, 2000 at 01:33 UTC ( [id://2264]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

How do I completely empty out an array?

Originally posted as a Categorized Question.

  • Comment on How do I completely empty out an array?

Replies are listed 'Best First'.
Re: How can I completely empty out a list;
by vroom (His Eminence) on Jan 25, 2000 at 22:08 UTC
    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.

Re: How do I completely empty out an array?
by ysth (Canon) on Nov 04, 2003 at 07:32 UTC
    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.
Re: How do I completely empty out an array?
by DigitalKitty (Parson) on May 27, 2002 at 05:49 UTC
    This would also work:
    splice( @array );
Re: How do I completely empty out an array?
by Anonymous Monk on Dec 15, 2000 at 00:24 UTC
    bleck

    Originally posted as a Categorized Answer.

Re: How do I completely empty out an array?
by Roger (Parson) on Sep 10, 2003 at 03:07 UTC
      But this doesn't change the size of @array. To do this, you can use $#array = 0;.

      daniel.

      Update: Oops, this is wrong (thanks to davido for pointing this out). Correct is $#array = -1;.
        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

        ~~(;#>_<#;)~~

        My mistake, you are right, it does not work, it will only replace all the entries of the array with undef's and the array size will remain the same. Just another of my stupid mistakes!
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-07-16 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.