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 Celtic" => { year => 2007,
  40. owner => "Wyc Grousbeck",
  41. captain => "Paul Pierce",
  42. champs => "Yes",
  43. notes => "Banner 17",
  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%-10s \t%-25s \t%-5 \t%-50 \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%-5s \t%-50s \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\n", "Team", "Year", "Owner", "Captain");
  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 \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>SuperHero Teams</H1>\n";
  98. print "<table border=1>\n";
  99. print "<tr><th>Team</th><th>Year</th><th>Owner</th><th>Captain</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.  
  106. print "<tr><td>$teamName</td><td>$year</td><td>$owner</td><td>$captain</td></tr>\n";
  107. }
  108. print "</table>\n";
  109. print "</body>\n";
  110. print "</html>\n";
  111.  
  112.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout

My Team - sorted by Team Name ascending:

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

Boston Celtics       	0      	           	                          	      	                                                   

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                   	
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>SuperHero Teams</H1>
<table border=1>
<tr><th>Team</th><th>Year</th><th>Owner</th><th>Captain</th></tr>
<tr><td>Boston Bruins</td><td>2011</td><td>J. Jacups</td><td>Zdeno Charra</td></tr>
<tr><td>Boston Celtic</td><td>2007</td><td>Wyc Grousbeck</td><td>Paul Pierce</td></tr>
<tr><td>Boston Celtics</td><td></td><td></td><td></td></tr>
<tr><td>Boston Redsox</td><td>2007</td><td>John Henry</td><td>Jason Varitek</td></tr>
<tr><td>New England Patriots</td><td>2018</td><td>Kraft</td><td>Tom Brady</td></tr>
<tr><td>USA Hockey</td><td>1980</td><td>USA Hockey</td><td>Mike Eruzione</td></tr>
</table>
</body>
</html>