sub printIndex { for my $book ( sort keys %ibIndex ) { # notice the use of sort in the next line for my $keyword ( sort keys( %{ $ibIndex{ $book } } ) { my $pages = join( ', ', @{ $ibIndex{ $book }->{ $keyword } } ); } } }