in reply to Problem with printing a Hash of Hashes in order
To help other beginner perl programmers, for the final use of this program, I ended up using a hash that used constants as the Keys as well.
This caused me some more stress as Constants don't really work well with what I was planning to do. How I solved this was to move the constants to a seperate file (this was going to be the original plan as I have a seperate perl module in my project that holds all of the constants).
For easy reference for programmers who are looking at using this method for making a HTML menu (or other uses here is the dummy final code I created:
#!c:\Perl\bin\perl use warnings; use strict; use diagnostics; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use Tie::IxHash; use CONSTANTS; tie my %menuHash, 'Tie::IxHash',CONSTANTS::TOP_MENU1_ => ordered_ha +sh_ref( CONSTANTS::TM1_SUB1_ => CONSTANTS::KANRI_SCREEN_04_1, CONSTANTS::TM1_SUB2_ => CONSTANTS::KANRI_SCREEN_04_2, CONSTANTS::TM1_SUB3_ => CONSTANTS::KANRI_SCREEN_04_3, CONSTANTS::TM1_SUB4_ => CONSTANTS::KANRI_SCREEN_04_4 ), CONSTANTS::TOP_MENU2_ => ordered_hash_ref( CONSTANTS::TM2_SUB1_ + => CONSTANTS::KANRI_SCREEN_05_1 ), CONSTANTS::TOP_MENU3_ => ordered_hash_ref( CONSTANTS::TM3_SUB1_ + => CONSTANTS::KANRI_SCREEN_06_1 ), CONSTANTS::TOP_MENU4_ => ordered_hash_ref( CONSTANTS::TM4_SUB1_ + => CONSTANTS::KANRI_SCREEN_07_1, CONSTANTS::TM4_SUB2_ => CONSTANTS::KANRI_SCREEN_07_2, CONSTANTS::TM4_SUB3_ => CONSTANTS::KANRI_SCREEN_07_3, CONSTANTS::TM4_SUB4_ => CONSTANTS::KANRI_SCREEN_07_4, CONSTANTS::TM4_SUB5_ => CONSTANTS::KANRI_SCREEN_07_5 ), CONSTANTS::TOP_MENU5_ => ordered_hash_ref( CONSTANTS::TM5_SUB1_ + => CONSTANTS::KANRI_SCREEN_08_1 ); my $cgi = new CGI; sub PrintMenu { my $upperMenu = shift; print "<hr>\n"; print $cgi->start_table( { -width => '100%', -border => 0 }); print "\n"; print '<tr align="center">'."\n"; for my $mainMenuItem ( keys %menuHash ) { print "<th>"; print $mainMenuItem; print "</th>\n"; } print "</tr>\n"; print $cgi->end_table(); print "<hr>\n"; print $cgi->start_table( { -width => '100%', -border => 0 }); print '<tr align="center">'."\n"; for my $subMenuItem ( keys %{ $menuHash{$upperMenu} } ) { print "<th>"; print $cgi->a( {-href => $menuHash{$upperMenu}{$subMenuItem +}, -target => '_self' }, "$subMenuItem" ); print "</th>\n"; } print "</tr>\n"; print $cgi->end_table(); print "\n"; print "<hr>\n"; } PrintMenu( CONSTANTS::TOP_MENU1_ );
And the code for the CONSTANTS.pm file is:
#!c:\Perl\bin\perl use warnings; use strict; package CONSTANTS; use constant { TOP_MENU1_ => 'TopMenu1', TOP_MENU2_ => 'TopMenu2', TOP_MENU3_ => 'TopMenu3', TOP_MENU4_ => 'TopMenu4', TOP_MENU5_ => 'TopMenu5', TM1_SUB1_ => 'SubMenu11', TM1_SUB2_ => 'SubMenu12', TM1_SUB3_ => 'SubMenu13', TM1_SUB4_ => 'SubMenu14', TM2_SUB1_ => 'SubMenu21', TM3_SUB1_ => 'SubMenu31', TM4_SUB1_ => 'SubMenu41', TM4_SUB2_ => 'SubMenu42', TM4_SUB3_ => 'SubMenu43', TM4_SUB4_ => 'SubMenu44', TM4_SUB5_ => 'SubMenu45', TM5_SUB1_ => 'SubMenu51', };
I hope this is helpful to people who read this with the same questions I had.
|
|---|