If you phrase your problem as: "how do I construct a new list of hrefs that excludes hrefs with certain IDs?", then the grep solution becomes much more obvious.

# build a hash that contains only excluded IDs my %exclude = map { ( $_->{id} => 1 ) } @AoH_one; # now keep only those that are not excluded: my @keepers = grep { not $exclude{ $_->{id} } } @AoH_all;

If you actually want to replace @AoH_all with this new list, there is nothing wrong with just assigning back into the same variable:

@AoH_all = grep { not $exclude{ $_->{id} } } @AoH_all;

As liz points out, you can use splice if you traverse the indexes in reverse order. You can also track the number of elements remaining.

# use liz's technique foreach my $i ( reverse 0 .. $#AoH_all ) { if ( $exclude{ $AoH_all[$i]{id} } ) { splice @AoH_all, $i, 1; } } # keep track of things manually my $n_elt = @AoH_all; for ( my $i = 0; $i < $n_elt; ++$i ) { if ( $exclude{ $AoH_all[$i]{id} } ) { splice @AoH_all, $i, 1; --$n_elt; # whoops, bug here, see update... } } # finally, perl updates the length of @AoH_all for us, so: for ( my $i = 0; $i < @AoH_all; ++$i ) { if ( $exclude{ $AoH_all[$i]{id} } ) { splice @AoH_all, $i, 1; # whoops, bug here, see update... } }

Update

Both of the last two loops have a serious error. After you splice out an element, everything else is shifted down — but the value shifted into $AoH_all[$i] is never examined again. Oops. We can either subvert the increment, or we can switch to only increment if we didn't do any replacement:

# adjust $i after replacement so it is reexamined: for ( my $i = 0; $i < @AoH_all; ++$i ) { if ( $exclude{ $AoH_all[$i]{id} } ) { splice @AoH_all, $i, 1; --$i; } } # or, only increment $i if we didn't do a replacement: for ( my $i = 0; $i < @AoH_all; ) { if ( $exclude{ $AoH_all[$i]{id} } ) { splice @AoH_all, $i, 1; } else { ++$i; } }

In reply to Re: Splice of ref to AoH not removing element by tkil
in thread Splice of ref to AoH not removing element by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.