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

Hello Monks,

My question is short: How can I delete hashes form an array of hashes?
I want to delete them all and then start puting new hashes in the array.

Thanks.

Replies are listed 'Best First'.
Re: delete array of hashes
by davido (Cardinal) on Sep 16, 2004 at 16:11 UTC

    Maybe you can avoid the "delete them all" step if you carefully plan with lexical scoping.

    { my @array = ( 1, 2, 3, 4, 5 ); print "\@array is in scope: @array\n"; } print "\@array is out of scope: @array\n";

    ...or...

    foreach ( 1, 2, 3 ) { my @array; print "Iteration $_: \@array has just been declared in scope.\n"; @array = ( int(rand(5)), int(rand(5)), int(rand(5)) ); print "\@array has been assigned: @array\n"; print "\@array is now falling out of scope; contents gone forever. +\n"; }

    Sometimes it's all in how you look at the problem. Emptying out an array might just be an indication that you need to be getting more out of lexical scoping. You can read up on the subject in perlintro, and perlsyn.


    Dave

Re: delete array of hashes
by Zed_Lopez (Chaplain) on Sep 16, 2004 at 15:46 UTC
    @array = ();

    or

    $#array = -1;

    will empty your array. Or you could just assign another list to the array and skip the emptying step.

    @array = ($newhashref1, $newhashref2);
      Yes but...
      eg. Array had 3 elements (hashes).
      Then I delete the array.
      Then I continue pushing new elements in the array (push(@arr,{});) but it seems that the first 3 places are still occupated and my new hashes start populating positions 4, 5, ... in the array.
      Any ideas?

        I don't think that can be true if you empty the array as you have been shown above. Perhaps you can show us the relevant parts of your code.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: delete array of hashes
by Random_Walk (Prior) on Sep 16, 2004 at 15:49 UTC