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
prints fine:#!/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";
BUT I change @AoH_one to: name => "Bill", id => 1 it prints (note the extra {} ):$VAR1 = 0; $VAR2 = [{'id' =>1, 'name' => 'Bill'}];
Instead of splicing, tried:$VAR1 = 1; $VAR2 = [{'id' =>3, 'name' => 'Mike'}, {} ];
and it Dumped:delete $AoH_dup->[$j]{'name'}; delete $AoH_dup->[$j]{'id'}; delete $AoH_dup->[$j];
Not any better for my final application. Thanks again.$VAR1 = 1; $VAR2 = [ undef,{'id' =>3, 'name' => 'Mike'} ];
In reply to Splice of ref to AoH not removing element by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |