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

Boston Celtics       	0      	           	                          	%-5 	%-50 

Boston Redsox        	2007   	John Henry 	Jason Varitek             	%-5 	%-50 

New England Patriots 	2018   	Kraft      	Tom Brady                 	%-5 	%-50 

USA Hockey           	1980   	USA Hockey 	Mike Eruzione             	%-5 	%-50 


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       	0      	           	                          	      	                                                   

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></td><td></td><td></td><td></td><td></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>