fork(1) download
  1. #!/usr/bin/perl
  2. # Perl Assignment - Hash of Hashes
  3. # J Student
  4.  
  5. # Teams using a Hash of Hashes
  6.  
  7. # Sport Activity Teams Year Owner Leader Famous Popularity
  8.  
  9. # Badminton 1934 Duke of Beaufort Poul-Erik Høyer Larsen Lin Dan 220,000,000
  10. # Golf 1894 PGA of America Professional Golfers' Association Tiger Woods 66,600,000
  11. # Yoga 1975 Patanjali Guruji Paramanhansa Yogananda 300,000,000
  12. # Running 1967 Ancient Greece Running Industry Association Dean Karnazes 60,000,000
  13. # Bowling 1988 US Bowling Congress Walter Ray Williams Jr Earl Anthony 100,000,000
  14.  
  15. # I have created the following array:
  16.  
  17. @teams = ("Badminton", "Golf", "Yoga", "Running" , "Bowling");
  18.  
  19. # and the following Hash of Hashes:
  20.  
  21. %myTeams = ( "Badminton" => { yearBorn => 1934,
  22. owner => "Duke of Beaufort",
  23. leader => "Poul-Erik Høyer Larsen",
  24. famous => "Lin Dan",
  25. popularity => "220M"
  26. },
  27. "Golf" => { yearBorn => 1894,
  28. owner => "PGA of America",
  29. leader => "Professional Golfers' Association",
  30. famous => "Tiger Woods",
  31. popularity => "66M"
  32. },
  33. "Yoga" => { yearBorn => 1975,
  34. owner => "Patanjali",
  35. leader => "Guruji",
  36. famous => "Paramanhansa Yogananda",
  37. popularity => "300M"
  38. },
  39. "Running" => { yearBorn => 1967,
  40. owner => "Ancient Greece",
  41. leader => "Running Industry Association",
  42. famous => "Dean Karnazes",
  43. popularity => "60M"
  44. },
  45. "Bowling" => { yearBorn => 1988,
  46. owner => "US Bowling Congress",
  47. leader => "Walter Ray Williams Jr",
  48. famous => "Earl Anthony",
  49. popularity => "100M"
  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("%-10s \t%-6s \t%-20s \t%-30s \t%-19s \t%-10s", "Team", "Year", "Owner", "Leader", "Famous", "Popularity");
  59.  
  60. @sortedKeys = sort (@teams);
  61.  
  62. for $teamName (@sortedKeys) {
  63. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  64. $owner = $myTeams{$teamName}{'owner'};
  65. $leader = $myTeams{$teamName}{'leader'};
  66. $famous = $myTeams{$teamName}{'famous'};
  67. $popularity = $myTeams{$teamName}{'popularity'};
  68.  
  69. printf("%-10s \t%-6s \t%-20s \t%-30s \t%-19s \t%-10s \n", $teamName, $yearBorn, $owner, $leader, $famous, $popularity);
  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("%-10s \t%-10s \t%-10s \t%-10s \t%-10s \t%-10s \n", "Team", "Year", "Owner");
  78.  
  79. @reverseKeys = reverse (@sortedKeys);
  80.  
  81. for $teamName (@reverseKeys) {
  82. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  83. $owner = $myTeams{$teamName}{'owner'};
  84.  
  85. printf("%-10s \t%-10s \t%-10s \n", $teamName, $yearBorn, $owner);
  86. print "\n";
  87.  
  88. }
  89.  
  90. printf("\t%-5s \n", "Leader");
  91. for $teamName (@reverseKeys) {
  92. $leader = $myTeams{$teamName}{'leader'};
  93.  
  94. printf("%-5s \n", $leader);
  95. print "\n";
  96. }
  97.  
  98. printf("\t%-5s \n", "Famous");
  99. for $teamName (@reverseKeys) {
  100. $famous = $myTeams{$teamName}{'famous'};
  101.  
  102. printf("%-5s \n", $famous);
  103. print "\n";
  104. }
  105.  
  106. printf("\t%-5s \n", "Popularity");
  107. for $teamName (@reverseKeys) {
  108. $popularity = $myTeams{$teamName}{'popularity'};
  109.  
  110. printf("%-5s \n", $popularity);
  111. print "\n";
  112. }
  113.  
  114.  
  115. print "\n\nHTML Page containing information on my Team:\n\n";
  116.  
  117. print "<html>\n";
  118. print "<head>\n";
  119. print "<title>My Team</title>";
  120. print "</head>\n";
  121. print "<body>\n";
  122. print "<H1>Sport Activity Teams</H1>\n";
  123. print "<table border=1>\n";
  124. print "<tr><th>Team</th><th>Year</th><th>Owner</th><th>Leader</th><th>Famous</th><th>Popularity</th></tr>\n";
  125.  
  126. for $teamName (sort keys %myTeams ) {
  127. $yearBorn = $myTeams{$teamName}{'yearBorn'};
  128. $owner = $myTeams{$teamName}{'owner'};
  129. $leader = $myTeams{$teamName}{'leader'};
  130. $famous = $myTeams{$teamName}{'famous'};
  131. $popularity = $myTeams{$teamName}{'popularity'};
  132.  
  133. print "<tr><td>$teamName</td><td>$yearBorn</td><td>$owner</td><td>$leader</td><td>$famous</td><td>$popularity</td></tr>\n";
  134. }
  135. print "</table>\n";
  136. print "</body>\n";
  137. print "</html>\n";
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout

My Team - sorted by Team Name ascending:

Team       	Year   	Owner                	Leader                         	Famous              	PopularityBadminton  	1934   	Duke of Beaufort     	Poul-Erik Høyer Larsen        	Lin Dan             	220M       

Bowling    	1988   	US Bowling Congress  	Walter Ray Williams Jr         	Earl Anthony        	100M       

Golf       	1894   	PGA of America       	Professional Golfers' Association 	Tiger Woods         	66M        

Running    	1967   	Ancient Greece       	Running Industry Association   	Dean Karnazes       	60M        

Yoga       	1975   	Patanjali            	Guruji                         	Paramanhansa Yogananda 	300M       


My Team - sorted by Team Name decending:

Team       	Year       	Owner      	           	           	           
Yoga       	1975       	Patanjali  

Running    	1967       	Ancient Greece 

Golf       	1894       	PGA of America 

Bowling    	1988       	US Bowling Congress 

Badminton  	1934       	Duke of Beaufort 

	Leader 
Guruji 

Running Industry Association 

Professional Golfers' Association 

Walter Ray Williams Jr 

Poul-Erik Høyer Larsen 

	Famous 
Paramanhansa Yogananda 

Dean Karnazes 

Tiger Woods 

Earl Anthony 

Lin Dan 

	Popularity 
300M  

60M   

66M   

100M  

220M  



HTML Page containing information on my Team:

<html>
<head>
<title>My Team</title></head>
<body>
<H1>Sport Activity Teams</H1>
<table border=1>
<tr><th>Team</th><th>Year</th><th>Owner</th><th>Leader</th><th>Famous</th><th>Popularity</th></tr>
<tr><td>Badminton</td><td>1934</td><td>Duke of Beaufort</td><td>Poul-Erik Høyer Larsen</td><td>Lin Dan</td><td>220M</td></tr>
<tr><td>Bowling</td><td>1988</td><td>US Bowling Congress</td><td>Walter Ray Williams Jr</td><td>Earl Anthony</td><td>100M</td></tr>
<tr><td>Golf</td><td>1894</td><td>PGA of America</td><td>Professional Golfers' Association</td><td>Tiger Woods</td><td>66M</td></tr>
<tr><td>Running</td><td>1967</td><td>Ancient Greece</td><td>Running Industry Association</td><td>Dean Karnazes</td><td>60M</td></tr>
<tr><td>Yoga</td><td>1975</td><td>Patanjali</td><td>Guruji</td><td>Paramanhansa Yogananda</td><td>300M</td></tr>
</table>
</body>
</html>