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

Usually, when I want to delete a list, I do: while ($z=pop(@mylist)) {}; Is there a faster, more elegant way of doing this? I want to destroy the list completely (without an interative approach)? I've searched my Perl book, but can't find anything. thx

Replies are listed 'Best First'.
Re: Elegant way of destroying a list
by turnstep (Parson) on Jan 23, 2001 at 21:56 UTC
      Thanks! :)
Re: Elegant way of destroying a list
by davorg (Chancellor) on Jan 23, 2001 at 21:57 UTC
Re: Elegant way of destroying a list
by Adam (Vicar) on Jan 23, 2001 at 22:09 UTC
    turnstep already pointed you at undef, but I'm left wondering, "Why do you want to destroy the list?" Ok, I know the most obvious answer, "To reclaim the memory," but that leaves me thinking that you are doing something else wrong. Think about it, if you have a list, and you don't need it anymore, shouldn't Perl take care of it for you? Well, yes, but only if you tell Perl to do so. And the way to do this is to scope your array, so that when you are done with it, it goes out of scope. Now if you are not already doing this, then you are also not using strict. It is never too late to start using this great tool.

    But Adam, you say, I am scoping the array using my, I just wanted to reuse the array inside the same scope. Well, don't. But if you are just writing a hack, and good programming isn't part of it, then in that case, use undef, or set the array to an empty array, or set the index of the last element to -1.

    $#arrayName = -1;
      Usually I try to pop the elements as I use them, so there's no additional cleanup needed in the end, or if it's at the end of the script, there's not really a need to manually delete it. Here's the script I was writing, where I was needing it.

      while (<infp>) { if (/^%!PS-Adobe/) #Start of Page Marker { if ($found_match) { @linelist = reverse @linelist; while ($line = pop(@linelist)) { print $line; } $found_match = 0; } else { #Empty List undef @linelist; } } elsif(/^%%Title/) { for $z(@figlist) { if (/$z[A-Za-z]*\.eps/i) { $found_match = 1; } } } push @linelist, $_; }

      Basically it parse a PostScript file, ripping out only the pages that match a certain search criteria (@figlist)

      I guess a different design would have prevented the need to delete the list. I never really used the "strict" keyword, but I'm glad you brought it up, it does help enforce good coding.

      Thanks for your response!

(tye)Re: Elegant way of destroying a list
by tye (Sage) on Jan 24, 2001 at 02:08 UTC

    Note that undef @list is usually a worse idea than @list= () since the former involves free()ing the memory for the list itself which will probably just have to reallocated again when an item is again added to the list.

            - tye (but my friends call me "Tye")
Re: Elegant way of destroying a list
by Fastolfe (Vicar) on Jan 23, 2001 at 23:27 UTC
    Set the array to an empty list:
    @array = (); # @array is an array, () is an empty list
    Similarly, arrays can be initialized by setting them to a populated list:
    @array = (1, 2, 3);