SPUG: Slice of HashOfHash
Jacinta Richardson
jarich at perltraining.com.au
Fri Nov 17 17:03:13 PST 2006
Forgot something!
> # ---------- ---------- ----------
> # loop through different hash has
> # that has names to look up in big HoH
> # ---------- ---------- ----------
> for my $name ( sort keys %names )
> {
> $name = lc ( trim ( $name ) );
> print "\n\t\t$name \n";
>
> # ---------- ---------- ----------
> # Get a subset of all our data based on the name
> # ---------- ---------- ----------
> %subHoH = (); # start empty
> while ( my ( $key, $obj ) = each %HoH )
> {
> $subHoH{$key} = $obj if ( $obj->{'name '} &&
> $obj->{'name '} eq $name );
> }
Here you build a hash. Since you then turn that into a list of keys and treat
it like an array maybe it would be better just to build a list anyway:
my @subAoH;
while( my $key, $obj ) = each %HoH )
{
push @subAoH = $obj if ...
}
> print Dumper ( \%subHoH ) if DEBUG2;
>
> # ---------- ---------- ----------
> # print out sorted by level and id for only this name
> # ---------- ---------- ----------
> foreach my $me ( sort { $subHoH{$a}->{'level'} <=>
> $subHoH{$b}->{'level'} ||
> $subHoH{$a}->{'id'} <=>
> $subHoH{$b}->{'id'}
> } keys %subHoH )
Now we don't need to get the keys out, so this becomes:
foreach my $me ( sort { $a->{level} <=> $b->{level}
|| $a->{id } <=> $b->{id }
} @subAoH )
{
if( $me->{level} == 0 )
{
print_parent( $me->{id} ) if $me->{id} > 0;
}
}
It helps simplify the code. :) (Untested, please verify.)
All the best,
J
More information about the spug-list
mailing list