in reply to removing a hash from an array of hashes

The tricky thing about using splice and a for loop is that you are altering the length of the array within the loop, so you need to perform a little hackery:
use strict; use warnings; use Data::Dumper; my @successarray; for my $value (182,1000,201,300,124,1000,201,300,124) { push @successarray, {ydayopen => $value} } my $fromopenmove = 200; for (my $i = 0;$i < @successarray;$i++) { if ($successarray[$i]->{'ydayopen'} > $fromopenmove) { splice(@successarray,$i,1); $i--; # This is the part that you might not think of } } print Dumper(\@successarray);

Replies are listed 'Best First'.
Re^2: removing a hash from an array of hashes
by BrowserUk (Patriarch) on Dec 13, 2010 at 16:48 UTC

    Another way to do that safely is to process the array backward:

    @x = 1..10;; for my $i ( reverse 0 .. $#x ){ $x[ $i ] & 1 and splice @x, $i, 1; };; print @x;; 2 4 6 8 10

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: removing a hash from an array of hashes
by Conal (Beadle) on Dec 13, 2010 at 17:01 UTC
    yeah. It was the decrement of the $i index count that i had overlooked in my experiments with splice

    thanks for taking the time to help me along.

    conal.