fork download
  1. #!/usr/bin/perl
  2.  
  3. # Perl Assignment - Hash of Hashes
  4. # Bill Davern
  5. # Assignment 8
  6.  
  7. # Teams using a Hash of Hashes
  8.  
  9. # Sports Team Year Owner Captain Champions Notes
  10.  
  11. # Justice Society 1940 DC Hawkman
  12. # Justice League 1960 DC Superman
  13. # Fantastic Four 1961 Marvel Mr. Fantastic
  14. # Avengers 1963 Marvel Captain America
  15. # X-Men 1963 Marvel Professor X
  16.  
  17. # I have created the following array:
  18.  
  19. @teams = ("Boston Bruins", "New England Patriots", "USA Hockey", "Boston Celtics" , "Boston Redsox");
  20.  
  21. # and the following Hash of Hashes:
  22.  
  23. %myTeams = ( "Boston Bruins" => { year => 2011,
  24. owner => "J. Jacups",
  25. captain => "Zdeno Charra",
  26. champs => "Yes",
  27. notes => "More of a gang then a team!",
  28. },
  29. "New England Patriots" => { year => 2018,
  30. owner => "Kraft",
  31. captain => "Tom Brady",
  32. champs => "Yes",
  33. notes => "Best Comeback Ever!",
  34. },
  35. "USA Hockey" => { year => 1980,
  36. owner => "USA Hockey",
  37. captain => "Mike Eruzione",
  38. champs => "Yes",
  39. notes => "Greatest Sporting event EVER!",
  40. },
  41. "Boston Redsox" => { year => 2007,
  42. owner => "John Henry",
  43. captain => "Jason Varitek",
  44. champs => "Yes",
  45. notes => "Can you say Sweep",
  46. },
  47. "Boston Celtics" => { year => 2008,
  48. owner => "Wic Grousbeck",
  49. captain => "Paul Pierce",
  50. champs => "Yes",
  51. notes => "Banner 17",
  52. },
  53.  
  54. );
  55.  
  56. # To print out sorted Team information in the Hash of Hashes (ascending order):
  57.  
  58. print ("\n\nMy Team - sorted by Team Name ascending:\n\n");
  59.  
  60. printf("%-20s \t%-6i \t%-15s \t%-25s \t%-5s \t%-50s \n", $teamName, $year, $owner, $captain, $champs, $notes);
  61.  
  62. @sortedKeys = sort (@teams);
  63.  
  64. for $teamName (@sortedKeys) {
  65. $year = $myTeams{$teamName}{'year'};
  66. $owner = $myTeams{$teamName}{'owner'};
  67. $captain = $myTeams{$teamName}{'captain'};
  68. $champs = $myTeams{$teamName}{'champs'};
  69. $notes = $myTeams{$teamName}{'notes'};
  70.  
  71. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-5s \t%-50s \n", $teamName, $year, $owner, $captain, $champs, $notes);
  72. print "\n";
  73. }
  74.  
  75. # To print out sorted Team information in the Hash of Hashes (descending order):
  76.  
  77. print ("\n\My Team - sorted by Team Name decending:\n\n");
  78.  
  79. printf("%-20s \t%-6s \t%-10s \t%-25s \t%-5s \t%-50s \t\n", "Team", "Year", "Owner", "Captain", "Champions", "Notes");
  80.  
  81. @reverseKeys = reverse (@sortedKeys);
  82.  
  83. for $teamName (@reverseKeys) {
  84. $year = $myTeams{$teamName}{'year'};
  85. $owner = $myTeams{$teamName}{'owner'};
  86. $captain = $myTeams{$teamName}{'captain'};
  87. $champs = $myTeams{$teamName}{'champs'};
  88. $notes = $myTeams{$teamName}{'notes'};
  89.  
  90. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-5s \t%-50s \n", $teamName, $year, $owner, $captain);
  91. print "\n";
  92. }
  93.  
  94. print "\n\nHTML Page containing information on my Team:\n\n";
  95.  
  96. print "<html>\n";
  97. print "<head>\n";
  98. print "<title>My Team</title>";
  99. print "</head>\n";
  100. print "<body>\n";
  101. print "<H1>Sports Teams</H1>\n";
  102. print "<table border=1>\n";
  103. print "<tr><th>Team</th><th>Year</th><th>Owner</th><th>Captain</th><th>Champions</th><th>Notes</th></tr>\n";
  104.  
  105. for $teamName (sort keys %myTeams ) {
  106. $year = $myTeams{$teamName}{'year'};
  107. $owner = $myTeams{$teamName}{'owner'};
  108. $captain = $myTeams{$teamName}{'captain'};
  109. $champs = $myTeams{$teamName}{'champs'};
  110. $notes = $myTeams{$teamName}{'notes'};
  111.  
  112. print "<tr><td>$teamName</td><td>$year</td><td>$owner</td><td>$captain</td><td>$champs</td><td>$notes</td></tr>\n";
  113. }
  114. print "</table>\n";
  115. print "</body>\n";
  116. print "</html>\n";
  117.  
  118.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout

My Team - sorted by Team Name ascending:

                     	0      	                	                          	      	                                                   
Boston Bruins        	2011   	J. Jacups  	Zdeno Charra              	Yes   	More of a gang then a team!                        

Boston Celtics       	2008   	Wic Grousbeck 	Paul Pierce               	Yes   	Banner 17                                          

Boston Redsox        	2007   	John Henry 	Jason Varitek             	Yes   	Can you say Sweep                                  

New England Patriots 	2018   	Kraft      	Tom Brady                 	Yes   	Best Comeback Ever!                                

USA Hockey           	1980   	USA Hockey 	Mike Eruzione             	Yes   	Greatest Sporting event EVER!                      


My Team - sorted by Team Name decending:

Team                 	Year   	Owner      	Captain                   	Champions 	Notes                                              	
USA Hockey           	1980   	USA Hockey 	Mike Eruzione             	      	                                                   

New England Patriots 	2018   	Kraft      	Tom Brady                 	      	                                                   

Boston Redsox        	2007   	John Henry 	Jason Varitek             	      	                                                   

Boston Celtics       	2008   	Wic Grousbeck 	Paul Pierce               	      	                                                   

Boston Bruins        	2011   	J. Jacups  	Zdeno Charra              	      	                                                   



HTML Page containing information on my Team:

<html>
<head>
<title>My Team</title></head>
<body>
<H1>Sports Teams</H1>
<table border=1>
<tr><th>Team</th><th>Year</th><th>Owner</th><th>Captain</th><th>Champions</th><th>Notes</th></tr>
<tr><td>Boston Bruins</td><td>2011</td><td>J. Jacups</td><td>Zdeno Charra</td><td>Yes</td><td>More of a gang then a team!</td></tr>
<tr><td>Boston Celtics</td><td>2008</td><td>Wic Grousbeck</td><td>Paul Pierce</td><td>Yes</td><td>Banner 17</td></tr>
<tr><td>Boston Redsox</td><td>2007</td><td>John Henry</td><td>Jason Varitek</td><td>Yes</td><td>Can you say Sweep</td></tr>
<tr><td>New England Patriots</td><td>2018</td><td>Kraft</td><td>Tom Brady</td><td>Yes</td><td>Best Comeback Ever!</td></tr>
<tr><td>USA Hockey</td><td>1980</td><td>USA Hockey</td><td>Mike Eruzione</td><td>Yes</td><td>Greatest Sporting event EVER!</td></tr>
</table>
</body>
</html>