bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
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'} ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splice of ref to AoH not removing element
by chromatic (Archbishop) on May 29, 2004 at 21:34 UTC | |
by liz (Monsignor) on May 30, 2004 at 12:59 UTC | |
by bradcathey (Prior) on May 30, 2004 at 21:29 UTC | |
by chromatic (Archbishop) on May 30, 2004 at 22:11 UTC | |
by bradcathey (Prior) on May 30, 2004 at 23:14 UTC | |
by tkil (Monk) on May 31, 2004 at 02:53 UTC | |
|
Re: Splice of ref to AoH not removing element
by tkil (Monk) on May 31, 2004 at 03:03 UTC |