Fellow Monasterians,

I'm stumped, and I'm sure it's something obvious I'm overlooking, or just not understanding. The object is to delete a matching element in a larger AoH. If I splice off the last match, or last element, the array is decreased properly and all is fine. If I splice out the first match, the hash key and value are deleted, but the empty hash or array element is still here. Tried deleting and got different results, but not the desired (see below). This arrayref is eventually going to be a list of options for an HTML dropdown box and I don't need the extra blank option(s) that are showing up. Ideas? TIA

#!/usr/bin/perl print "Content-type: text/plain\n\n"; use strict; use Data::Dumper; my @AoH_all = ( { name => "Bill", id => 1 }, { name => "Mike", id => 3 } ); my @AoH_one = ( { name => "Bill", id => 1 } ); my $AoH_all = \@AoH_all; my $AoH_dup = \@AoH_all; my $AoH_one = \@AoH_one; for my $j (0 .. $#$AoH_all) { for my $i (0 ..$#$AoH_one) { if ($AoH_one->[$i]{'id'} == $AoH_all->[$j]{'id'}) { splice( @{$AoH_dup}, $j, 1 ); } } } print Dumper($#$AoH_dup, $AoH_dup )."\n";
prints fine:
$VAR1 = 0; $VAR2 = [{'id' =>1, 'name' => 'Bill'}];
BUT I change @AoH_one to: name => "Bill", id => 1 it prints (note the extra {} ):
$VAR1 = 1; $VAR2 = [{'id' =>3, 'name' => 'Mike'}, {} ];
Instead of splicing, tried:
delete $AoH_dup->[$j]{'name'}; delete $AoH_dup->[$j]{'id'}; delete $AoH_dup->[$j];
and it Dumped:
$VAR1 = 1; $VAR2 = [ undef,{'id' =>3, 'name' => 'Mike'} ];
Not any better for my final application. Thanks again.

—Brad
"A little yeast leavens the whole dough."

In reply to 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.