foreach my $page (qw/NT UNIX MAIL/) { # open output html file for this page, print the header, # maybe you open a second file for the html to "#include", # then... foreach my $class (sort grep {$classname{$_} eq $page} keys %classHash ) { foreach my $status (sort keys %{$classHash{$class}}) { # print the page content, etc. } } # print the footer and close this output file(s). }