fork download
  1. #!/usr/bin/perl
  2.  
  3. # Perl Assignment - Hash of Hashes
  4. # J Student
  5. # Teams using a Hash of Hashes
  6.  
  7. # Formula 1 Team Year Owner/Parent Company Team Principal Country Championships
  8. # Mercedes 1970 Mercedes-Benz Toto Wolff Germany 8
  9. # Ferrari 1950 Ferrari N.V. Frederic Vasseur Italy 16
  10. # Red Bull Racing 2005 Red Bull GmbH Christian Horner Austria 6
  11. # McLaren 1966 McLaren Group Zak Brown United Kingdom 8
  12. # Alpine 1981 Renault Group Bruno Famin France 2
  13.  
  14. # I have created the following array:
  15. @teams = ("Mercedes", "Ferrari", "Red Bull Racing", "McLaren", "Alpine");
  16.  
  17. # and the following Hash of Hashes:
  18. %myTeams = ( "Mercedes" => { yearBorn => 1970,
  19. owner => "Mercedes-Benz",
  20. leader => "Toto Wolff",
  21. country => "Germany",
  22. championships => 8,
  23. },
  24. "Ferrari" => { yearBorn => 1950,
  25. owner => "Ferrari N.V.",
  26. leader => "Frederic Vasseur",
  27. country => "Italy",
  28. championships => 16,
  29. },
  30. "Red Bull Racing" => { yearBorn => 2005,
  31. owner => "Red Bull GmbH",
  32. leader => "Christian Horner",
  33. country => "Austria",
  34. championships => 6,
  35. },
  36. "McLaren" => { yearBorn => 1966,
  37. owner => "McLaren Group",
  38. leader => "Zak Brown",
  39. country => "United Kingdom",
  40. championships => 8,
  41. },
  42. "Alpine" => { yearBorn => 1981,
  43. owner => "Renault Group",
  44. leader => "Bruno Famin",
  45. country => "France",
  46. championships => 2,
  47. },
  48. );
  49.  
  50. # To print out sorted Team information in the Hash of Hashes (ascending order):
  51. print ("\n\nMy Team - sorted by Team Name ascending:\n\n");
  52. printf("%-20s \t%-6s \t%-15s \t%-20s \t%-15s \t%-6s \n", "Team", "Year", "Owner", "Leader", "Country", "Titles");
  53. @sortedKeys = sort (@teams);
  54. for $teamName (@sortedKeys) {
  55. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  56. $owner = $myTeams{$teamName}{'owner'};
  57. $leader = $myTeams{$teamName}{'leader'};
  58. $country = $myTeams{$teamName}{'country'};
  59. $championships = $myTeams{$teamName}{'championships'};
  60. printf("%-20s \t%-6i \t%-15s \t%-20s \t%-15s \t%-6i \n", $teamName, $yearBorn, $owner, $leader, $country, $championships);
  61. print "\n";
  62. }
  63.  
  64. # To print out sorted Team information in the Hash of Hashes (descending order):
  65. print ("\n\My Team - sorted by Team Name decending:\n\n");
  66. printf("%-20s \t%-6s \t%-15s \t%-20s \t%-15s \t%-6s \n", "Team", "Year", "Owner", "Leader", "Country", "Titles");
  67. @reverseKeys = reverse (@sortedKeys);
  68. for $teamName (@reverseKeys) {
  69. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  70. $owner = $myTeams{$teamName}{'owner'};
  71. $leader = $myTeams{$teamName}{'leader'};
  72. $country = $myTeams{$teamName}{'country'};
  73. $championships = $myTeams{$teamName}{'championships'};
  74.  
  75. printf("%-20s \t%-6i \t%-15s \t%-20s \t%-15s \t%-6i \n", $teamName, $yearBorn, $owner, $leader, $country, $championships);
  76. print "\n";
  77. }
  78.  
  79. print "\n\nHTML Page containing information on my Team:\n\n";
  80. print "<html>\n";
  81. print "<head>\n";
  82. print "<title>My Team</title>";
  83. print "</head>\n";
  84. print "<body>\n";
  85. print "<H1>Formula 1 Racing Teams</H1>\n";
  86. print "<table border=1>\n";
  87. print "<tr><th>Team</th><th>Year</th><th>Owner</th><th>Leader</th><th>Country</th><th>Championships</th></tr>\n";
  88. for $teamName (sort keys %myTeams ) {
  89. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  90. $owner = $myTeams{$teamName}{'owner'};
  91. $leader = $myTeams{$teamName}{'leader'};
  92. $country = $myTeams{$teamName}{'country'};
  93. $championships = $myTeams{$teamName}{'championships'};
  94. print "<tr><td>$teamName</td><td>$yearBorn</td><td>$owner</td><td>$leader</td><td>$country</td><td>$championships</td></tr>\n";
  95. }
  96. print "</table>\n";
  97. print "</body>\n";
  98. print "</html>\n";
  99.  
  100. print "\n\nXML file containing information on my Team - by Team Name ascending:\n\n";
  101. print "<?xml version=\"1.0\"?>\n";
  102. print "<teams>\n";
  103. for $teamName (sort keys %myTeams ) {
  104. print " <team>\n";
  105. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  106. $owner = $myTeams{$teamName}{'owner'};
  107. $leader = $myTeams{$teamName}{'leader'};
  108. $country = $myTeams{$teamName}{'country'};
  109. $championships = $myTeams{$teamName}{'championships'};
  110.  
  111. print " <teamName>$teamName</teamName>\n";
  112. print " <yearBorn>$yearBorn</yearBorn>\n";
  113. print " <owner>$owner</owner>\n";
  114. print " <leader>$leader</leader>\n";
  115. print " <country>$country</country>\n";
  116. print " <championships>$championships</championships>\n";
  117. print " </team>\n";
  118. }
  119. print "</teams>\n";
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout

My Team - sorted by Team Name ascending:

Team                 	Year   	Owner           	Leader               	Country         	Titles 
Alpine               	1981   	Renault Group   	Bruno Famin          	France          	2      

Ferrari              	1950   	Ferrari N.V.    	Frederic Vasseur     	Italy           	16     

McLaren              	1966   	McLaren Group   	Zak Brown            	United Kingdom  	8      

Mercedes             	1970   	Mercedes-Benz   	Toto Wolff           	Germany         	8      

Red Bull Racing      	2005   	Red Bull GmbH   	Christian Horner     	Austria         	6      


My Team - sorted by Team Name decending:

Team                 	Year   	Owner           	Leader               	Country         	Titles 
Red Bull Racing      	2005   	Red Bull GmbH   	Christian Horner     	Austria         	6      

Mercedes             	1970   	Mercedes-Benz   	Toto Wolff           	Germany         	8      

McLaren              	1966   	McLaren Group   	Zak Brown            	United Kingdom  	8      

Ferrari              	1950   	Ferrari N.V.    	Frederic Vasseur     	Italy           	16     

Alpine               	1981   	Renault Group   	Bruno Famin          	France          	2      



HTML Page containing information on my Team:

<html>
<head>
<title>My Team</title></head>
<body>
<H1>Formula 1 Racing Teams</H1>
<table border=1>
<tr><th>Team</th><th>Year</th><th>Owner</th><th>Leader</th><th>Country</th><th>Championships</th></tr>
<tr><td>Alpine</td><td>1981</td><td>Renault Group</td><td>Bruno Famin</td><td>France</td><td>2</td></tr>
<tr><td>Ferrari</td><td>1950</td><td>Ferrari N.V.</td><td>Frederic Vasseur</td><td>Italy</td><td>16</td></tr>
<tr><td>McLaren</td><td>1966</td><td>McLaren Group</td><td>Zak Brown</td><td>United Kingdom</td><td>8</td></tr>
<tr><td>Mercedes</td><td>1970</td><td>Mercedes-Benz</td><td>Toto Wolff</td><td>Germany</td><td>8</td></tr>
<tr><td>Red Bull Racing</td><td>2005</td><td>Red Bull GmbH</td><td>Christian Horner</td><td>Austria</td><td>6</td></tr>
</table>
</body>
</html>


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

<?xml version="1.0"?>
<teams>
 <team>
 <teamName>Alpine</teamName>
 <yearBorn>1981</yearBorn>
 <owner>Renault Group</owner>
 <leader>Bruno Famin</leader>
 <country>France</country>
 <championships>2</championships>
 </team>
 <team>
 <teamName>Ferrari</teamName>
 <yearBorn>1950</yearBorn>
 <owner>Ferrari N.V.</owner>
 <leader>Frederic Vasseur</leader>
 <country>Italy</country>
 <championships>16</championships>
 </team>
 <team>
 <teamName>McLaren</teamName>
 <yearBorn>1966</yearBorn>
 <owner>McLaren Group</owner>
 <leader>Zak Brown</leader>
 <country>United Kingdom</country>
 <championships>8</championships>
 </team>
 <team>
 <teamName>Mercedes</teamName>
 <yearBorn>1970</yearBorn>
 <owner>Mercedes-Benz</owner>
 <leader>Toto Wolff</leader>
 <country>Germany</country>
 <championships>8</championships>
 </team>
 <team>
 <teamName>Red Bull Racing</teamName>
 <yearBorn>2005</yearBorn>
 <owner>Red Bull GmbH</owner>
 <leader>Christian Horner</leader>
 <country>Austria</country>
 <championships>6</championships>
 </team>
</teams>