SPUG: Slice of HashOfHash
Jacinta Richardson
jarich at perltraining.com.au
Fri Nov 17 16:55:21 PST 2006
> # ---------- ---------- ----------
> # 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
You can achieve the same effect (especially since you "clean up subHoH" at the
end of this loop by writing:
my %subHoH;
here. It means that Perl will take care of clean-up and all the rest for you. :)
> while ( my ( $key, $obj ) = each %HoH )
> {
> $subHoH{$key} = $obj if ( $obj->{'name '} &&
> $obj->{'name '} eq $name );
> }
> 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 )
> {
> if ( $subHoH{$me}->{'level'} == 0 )
> {
> # ---------- ---------- ----------
> # recursive call, to walk through the subHoH and
> # print parent information based on this starting ID
> # ---------- ---------- ----------
> &print_parent ( $subHoH{$me}->{'id'} ) if
> $subHoH{$me}->{'id'} > 0;
Since Perl 5 came out, the need for prepending subroutine names with &s has
gone. Well except for some very few cases which hopefully you won't encounter.
Thus, this can be written:
print_parent( ... );
quite safely.
Otherwise, looks good.
All the best,
J
More information about the spug-list
mailing list