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

I have a while loop which loops 30 times. For each iterations i am poping values onto the array @table_cont. On the tenth and twentith iteration i do the following -
push @LoL ,[table_cont];
But i need to add one line to the code above. I need to reset the table_cont array so that it contains no elements. Thatway when i continue looping on the 11th and 21th iterations i start pushing onto an empty table_cont array. How do I do this, In C u'd set the array to NULL.

Replies are listed 'Best First'.
Re: deleting all the elements of an array
by Beatnik (Parson) on Dec 14, 2001 at 03:46 UTC
    @table_cont = ();

    Update: A very common mistake is to set the array to undef
    @table_cont = undef;

    Which stores 1 element in the array, containing undef (I believe this was a bug in early version of Tie::Array). Use the code specified above to really clear the array.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: deleting all the elements of an array
by chip (Curate) on Dec 14, 2001 at 09:53 UTC
    The undef() operator can clear variables of all types:

    undef $scalar; # $scalar = undef undef @array; # @array = () undef %hash; # %hash = ()

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: deleting all the elements of an array
by danboo (Beadle) on Dec 14, 2001 at 03:46 UTC
    Set it equal to the empty list:

    @table_cont = ();

    - danboo

Re: deleting all the elements of an array
by archen (Pilgrim) on Dec 14, 2001 at 04:59 UTC
    you can also clear an array by setting it's ending index to -1 like
    $#table_cont = -1;
    It's probably more intuitive to set it to an empty list though (as people have been saying)
Re: deleting all the elements of an array
by BazB (Priest) on Dec 14, 2001 at 03:51 UTC

    Try assigning the array to an empty list (), like so:

    @table_cont = ();

    Cheers.

    BazB.

    Update: Oops. Bit slow. Duplicate post.

Re: deleting all the elements of an array
by dorko (Prior) on Dec 14, 2001 at 05:00 UTC
    There is more than one way to do it. I like to:
    $#table_cont = -1;
    This clears the array by setting the number of elements in the array to -1.

    Cheers!

    Brent

    Update: Sometimes you win, sometimes you lose. Today I'm losing. Slow by a minute... Duplicate post.

      What you said works, but what you said for why it works doesn't. More specifically, what you assign to $#array is off by 1 from how many elements you leave in it.
      well if it makes you feel any better, I had to try to post 3 times before it finally got accepted. Seems like when you press ctrl+c in Mozilla in a form it randomly sends you back in history, and doesn't let you go foreward...
        You know, that made me smile and laugh a little. So yeah, it did make me feel a little better. ;-)

        Cheers!

        Brent

Re: deleting all the elements of an array
by Steve_p (Priest) on Dec 14, 2001 at 07:54 UTC
    Assuming @table_cont is you array, then
    @table_cont = ();
    
    would clear out the array.