Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have found myself in need to delete an array (I'm in a loop and I'd like to start this temporary array anew each time it iterates). I've never gone ahead and needed to delete an array before and now I'm asking for some wisdom. I remember at one time that delete @array will result in errors if under strict?

I just need to literally delete everything in it.

Hmm Just had the idea I could just loop the array and pop(). Is that the best way?

Replies are listed 'Best First'.
Re: How to delete an array
by kyle (Abbot) on May 16, 2007 at 18:52 UTC

    Just use an array lexically scoped to the body of the loop.

    while ( $looping_is_desirable ) { my @new_array_every_time; # work, work, work }

    In general, you can delete the contents of an array with undef.

Re: How to delete an array
by TedYoung (Deacon) on May 16, 2007 at 18:49 UTC
    @array = ();

    Will do the trick.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: How to delete an array
by swampyankee (Parson) on May 16, 2007 at 19:00 UTC

    Depending on your algorithm, you could try taking advantage of my and scoping; something like this:

    while($condition){ my @array; .. .. }
    That will restrict @array's scope to the loop.

    You can also use undef to undefine the array.

    Hmm Just had the idea I could just loop the array and pop(). Is that the best way?

    This depends on how the algorithm using the array is set up.

    emc

    Insisting on perfect safety is for people who don't have the balls to live in the real world.

    —Mary Shafer, NASA Dryden Flight Research Center