fork(1) download
  1. # Perl Assignment - Hash of Hashes
  2. # J Student
  3.  
  4. # Teams using a Hash of Hashes
  5.  
  6. # Superhero Team Year Owner Leader
  7.  
  8. # Justice Society 1940 DC Hawkman
  9. # Justice League 1960 DC Superman
  10. # Fantastic Four 1961 Marvel Mr. Fantastic
  11. # Avengers 1963 Marvel Captain America
  12. # X-Men 1963 Marvel Professor X
  13.  
  14. # I have created the following array:
  15.  
  16. @teams = ("Justice Society", "Justice League", "Fantastic Four", "Avengers" , "X-Men");
  17.  
  18. # and the following Hash of Hashes:
  19.  
  20. %myTeams = ( "Justice Society" => { yearBorn => 1940,
  21. owner => "DC",
  22. leader => "Hawkman",
  23. headquarters => "New York City",
  24. mission => "Fight injustice in society"
  25. },
  26. "Justice League" => { yearBorn => 1960,
  27. owner => "DC",
  28. leader => "Superman",
  29. headquarters => "Hall of Justice",
  30. mission => "Protect Earth from global threats"
  31. },
  32. "Fantastic Four" => { yearBorn => 1961,
  33. owner => "Marvel",
  34. leader => "Mr. Fantastic",
  35. headquarters => "Baxter Building",
  36. mission => "Explore space and dimensions"
  37. },
  38. "Avengers" => { yearBorn => 1963,
  39. owner => "Marvel",
  40. leader => "Captain America",
  41. headquarters => "Avengers Tower",
  42. mission => "Assemble heroes to protect humanity"
  43. },
  44. "X-Men" => { yearBorn => 1963,
  45. owner => "Marvel",
  46. leader => "Professor X",
  47. headquarters => "Xavier’s School",
  48. mission => "Promote coexistence between humans and mutants"
  49. },
  50.  
  51. );
  52.  
  53. # To print out sorted Team information in the Hash of Hashes (ascending order):
  54.  
  55. print ("\n\nMy Team - sorted by Team Name ascending:\n\n");
  56.  
  57. printf("%-20s \t%-6s \t%-10s \t%-25s \t%-20s \t%-40s \n", "Team", "Year", "Owner", "Leader", "Headquarters", "Mission");
  58.  
  59. @sortedKeys = sort (@teams);
  60.  
  61. for $teamName (@sortedKeys) {
  62. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  63. $owner = $myTeams{$teamName}{'owner'};
  64. $leader = $myTeams{$teamName}{'leader'};
  65. $headquarters = $myTeams{$teamName}{'headquarters'};
  66. $mission = $myTeams{$teamName}{'mission'};
  67.  
  68. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-20s \t%-40s \n", $teamName, $yearBorn, $owner, $leader, $headquarters, $mission);
  69. print "\n";
  70. }
  71.  
  72. # To print out sorted Team information in the Hash of Hashes (descending order):
  73.  
  74. print ("\n\My Team - sorted by Team Name decending:\n\n");
  75.  
  76. printf("%-20s \t%-6s \t%-10s \t%-25s \t%-20s \t%-40s \n", "Team", "Year", "Owner", "Leader", "Headquarters", "Mission");
  77.  
  78. @reverseKeys = reverse (@sortedKeys);
  79.  
  80. for $teamName (@reverseKeys) {
  81. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  82. $owner = $myTeams{$teamName}{'owner'};
  83. $leader = $myTeams{$teamName}{'leader'};
  84. $headquarters = $myTeams{$teamName}{'headquarters'};
  85. $mission = $myTeams{$teamName}{'mission'};
  86.  
  87. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-20s \t%-40s \n", $teamName, $yearBorn, $owner, $leader, $headquarters, $mission);
  88. print "\n";
  89. }
  90.  
  91. print "\n\nHTML Page containing information on my Team:\n\n";
  92.  
  93. print "<html>\n";
  94. print "<head>\n";
  95. print "<title>My Team</title>";
  96. print "</head>\n";
  97. print "<body>\n";
  98. print "<H1>SuperHero Teams</H1>\n";
  99. print "<table border=1>\n";
  100. print "<tr><th>Team</th><th>Year</th><th>Owner</th><th>Leader</th><th>Headquarters</th><th>Mission</th></tr>\n";
  101.  
  102. for $teamName (sort keys %myTeams ) {
  103. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  104. $owner = $myTeams{$teamName}{'owner'};
  105. $leader = $myTeams{$teamName}{'leader'};
  106. $headquarters = $myTeams{$teamName}{'headquarters'};
  107. $mission = $myTeams{$teamName}{'mission'};
  108.  
  109. print "<tr><td>$teamName</td><td>$yearBorn</td><td>$owner</td><td>$leader</td><td>$headquarters</td><td>$mission</td></tr>\n";
  110. }
  111. print "</table>\n";
  112. print "</body>\n";
  113. print "</html>\n";
  114.  
  115. # Optional Challenge: XML Output
  116.  
  117. print "\n\nXML file containing information on my Team - by Team Name ascending:\n\n";
  118.  
  119. print "<?xml version=\"1.0\"?>\n";
  120. print "<teams>\n";
  121.  
  122. for $teamName (sort keys %myTeams ) {
  123.  
  124. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  125. $owner = $myTeams{$teamName}{'owner'};
  126. $leader = $myTeams{$teamName}{'leader'};
  127. $headquarters = $myTeams{$teamName}{'headquarters'};
  128. $mission = $myTeams{$teamName}{'mission'};
  129.  
  130. print " <team>\n";
  131. print " <teamName>$teamName</teamName>\n";
  132. print " <yearBorn>$yearBorn</yearBorn>\n";
  133. print " <owner>$owner</owner>\n";
  134. print " <leader>$leader</leader>\n";
  135. print " <headquarters>$headquarters</headquarters>\n";
  136. print " <mission>$mission</mission>\n";
  137. print " </team>\n";
  138. }
  139. print "</teams>\n";
  140.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout

My Team - sorted by Team Name ascending:

Team                 	Year   	Owner      	Leader                    	Headquarters         	Mission                                  
Avengers             	1963   	Marvel     	Captain America           	Avengers Tower       	Assemble heroes to protect humanity      

Fantastic Four       	1961   	Marvel     	Mr. Fantastic             	Baxter Building      	Explore space and dimensions             

Justice League       	1960   	DC         	Superman                  	Hall of Justice      	Protect Earth from global threats        

Justice Society      	1940   	DC         	Hawkman                   	New York City        	Fight injustice in society               

X-Men                	1963   	Marvel     	Professor X               	Xavier’s School    	Promote coexistence between humans and mutants 


My Team - sorted by Team Name decending:

Team                 	Year   	Owner      	Leader                    	Headquarters         	Mission                                  
X-Men                	1963   	Marvel     	Professor X               	Xavier’s School    	Promote coexistence between humans and mutants 

Justice Society      	1940   	DC         	Hawkman                   	New York City        	Fight injustice in society               

Justice League       	1960   	DC         	Superman                  	Hall of Justice      	Protect Earth from global threats        

Fantastic Four       	1961   	Marvel     	Mr. Fantastic             	Baxter Building      	Explore space and dimensions             

Avengers             	1963   	Marvel     	Captain America           	Avengers Tower       	Assemble heroes to protect humanity      



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>Leader</th><th>Headquarters</th><th>Mission</th></tr>
<tr><td>Avengers</td><td>1963</td><td>Marvel</td><td>Captain America</td><td>Avengers Tower</td><td>Assemble heroes to protect humanity</td></tr>
<tr><td>Fantastic Four</td><td>1961</td><td>Marvel</td><td>Mr. Fantastic</td><td>Baxter Building</td><td>Explore space and dimensions</td></tr>
<tr><td>Justice League</td><td>1960</td><td>DC</td><td>Superman</td><td>Hall of Justice</td><td>Protect Earth from global threats</td></tr>
<tr><td>Justice Society</td><td>1940</td><td>DC</td><td>Hawkman</td><td>New York City</td><td>Fight injustice in society</td></tr>
<tr><td>X-Men</td><td>1963</td><td>Marvel</td><td>Professor X</td><td>Xavier’s School</td><td>Promote coexistence between humans and mutants</td></tr>
</table>
</body>
</html>


XML file containing information on my Team - by Team Name ascending:

<?xml version="1.0"?>
<teams>
 <team>
   <teamName>Avengers</teamName>
   <yearBorn>1963</yearBorn>
   <owner>Marvel</owner>
   <leader>Captain America</leader>
   <headquarters>Avengers Tower</headquarters>
   <mission>Assemble heroes to protect humanity</mission>
 </team>
 <team>
   <teamName>Fantastic Four</teamName>
   <yearBorn>1961</yearBorn>
   <owner>Marvel</owner>
   <leader>Mr. Fantastic</leader>
   <headquarters>Baxter Building</headquarters>
   <mission>Explore space and dimensions</mission>
 </team>
 <team>
   <teamName>Justice League</teamName>
   <yearBorn>1960</yearBorn>
   <owner>DC</owner>
   <leader>Superman</leader>
   <headquarters>Hall of Justice</headquarters>
   <mission>Protect Earth from global threats</mission>
 </team>
 <team>
   <teamName>Justice Society</teamName>
   <yearBorn>1940</yearBorn>
   <owner>DC</owner>
   <leader>Hawkman</leader>
   <headquarters>New York City</headquarters>
   <mission>Fight injustice in society</mission>
 </team>
 <team>
   <teamName>X-Men</teamName>
   <yearBorn>1963</yearBorn>
   <owner>Marvel</owner>
   <leader>Professor X</leader>
   <headquarters>Xavier’s School</headquarters>
   <mission>Promote coexistence between humans and mutants</mission>
 </team>
</teams>