fork download
  1. #!/usr/bin/perl
  2. # Perl Assignment - Hash of Hashes
  3. # J Student
  4.  
  5. # Spirits using a Hash of Hashes
  6.  
  7. # Spirit Name Distributor Type AlcVol Size Origin
  8.  
  9. # BeefEater Pernod Ricardo Gin 0.47 1litre London
  10. # Drambuie Hiram Walker Whisky 0.40 750ml Scotland
  11. # Chivas Regal Chivas Bros Whisky 0.40 750ml Scotland
  12. # The Balverie Carribean Cask William Grant Rum 0.43 750ml Scotland
  13. # Martini & Rossi Bacardi Inc Vermouth 0.16 375ml Italy
  14.  
  15. # I have created the following array:
  16.  
  17. @spirits = ("BeefEater", "Drambuie", "Chivas Regal", "The Balverie Carribean Cask" , "Martini & Rossi");
  18.  
  19. # and the following Hash of Hashes:
  20.  
  21. %mySpirits = ( "BeefEater" => { distributor => "Pernod Ricardo",
  22. type => "Gin",
  23. alcvol => 0.47,
  24. size => "1litre",
  25. origin => "England"
  26. },
  27. "Drambuie" => { distributor => "Hiram Walker",
  28. type => "Whisky",
  29. alcvol => 0.40,
  30. size => "750ml",
  31. origin => "Scotland"
  32. },
  33. "Chivas Regal" => { distributor => "Chivas Bros",
  34. type => "Whisky",
  35. alcvol => 0.40,
  36. size => "750ml",
  37. origin => "Scotland"
  38. },
  39. "The Balverie Carribean Cask" => { distributor => "William Grant",
  40. type => "Rum",
  41. alcvol => 0.43,
  42. size => "750ml",
  43. origin => "Scotland"
  44. },
  45. "Martini & Rossi" => { distributor=> "Barcadi Inc",
  46. type => "Vermouth",
  47. alcvol => 0.16,
  48. size => "750ml",
  49. origin => "Italy"
  50. },
  51.  
  52. );
  53.  
  54. # To print out sorted Team information in the Hash of Hashes (ascending order):
  55.  
  56. print ("\n\nMy Spirits - sorted by Spirit Name ascending:\n\n");
  57.  
  58. printf("%-20s \t%-6s \t%-10s \t%-25s \t%-10s \t%-10s \n", "Spirit Name", "Distributor", "Type", "AlcVol", "Size", "Origin");
  59.  
  60. @sortedKeys = sort (@spirits);
  61.  
  62. for $spiritName (@sortedKeys) {
  63. $distributor = $mySpirits{$spiritName}{'distributor'};
  64. $type = $mySpirits{$spiritName}{'type'};
  65. $alcvol = $mySpirits{$spiritName}{'alcvol'};
  66. $size = $mySpirits{$spiritName}{'size'};
  67. $origin = $mySpirits{$spiritName}{'origin'};
  68. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-10s \t%-10s \n",$spiritName, $distributor, $type, $alcvol, $size, $origin);
  69. print "\n";
  70. }
  71.  
  72.  
  73.  
  74. print "\n\nHTML Page containing information on my Team:\n\n";
  75.  
  76. print "<html>\n";
  77. print "<head>\n";
  78. print "<title>Product Registration</title>";
  79. print "</head>\n";
  80. print "<body>\n";
  81. print "<H1>Spirit List</H1>\n";
  82. print "<table border=1>\n";
  83. print "<tr><th>Spirit</th><th>Distributor</th><th>Type</th><th>Alc/Vol</th><th>Size</th><th>Origin</th></tr>\n";
  84.  
  85. for $spiritName (sort keys %mySpirits ) {
  86. $distributor = $mySpirits{$spiritName}{'distributor'};
  87. $type = $mySpirits{$spiritName}{'type'};
  88. $alcvol = $mySpirits{$spiritName}{'alcvol'};
  89. $size = $mySpirits{$spiritName}{'size'};
  90. $origin = $mySpirits{$spiritName}{'origin'};
  91. print "<tr><td>$spiritName</td><td>$distributor</td><td>$type</td><td>$alcvol</td><td>$size</td><td>$origin</td></tr>\n";
  92. }
  93. print "</table>\n";
  94. print "</body>\n";
  95. print "</html>\n";
  96.  
  97. print "\n\nXML file containing information on my Spirits - by Spirit Name ascending:\n\n";
  98.  
  99. print "<?xml version=\"1.0\"?>\n";
  100. print "<spirits>\n";
  101.  
  102. for $spiritName (sort keys %mySpirits ) {
  103.  
  104. print " <spirit>\n";
  105.  
  106. $distributor = $mySpirits{$spiritName}{'distributor'};
  107. $type = $mySpirits{$spiritName}{'type'};
  108. $alcvol = $mySpirits{$spiritName}{'alcvol'};
  109. $size = $mySpirits{$spiritName}{'size'};
  110. $origin = $mySpirits{$spiritName}{'origin'};
  111. print " \n";
  112.  
  113. print " < spiritName>$ spiritName</ spiritName>\n";
  114. print " < distributor>$distributor</ distributor>\n";
  115. print " < type>$type</ type>\n";
  116. print " < alcvol>$alcvol</ alcvol>\n";
  117. print " < size>$size</ size>\n";
  118. print " < origin>$origin</ origin>\n";
  119. print " </spirit>\n";
  120. }
  121. print "</spirits>\n";
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout

My Spirits - sorted by Spirit Name ascending:

Spirit Name          	Distributor 	Type       	AlcVol                    	Size       	Origin     
BeefEater            	0      	Gin        	0.47                      	1litre     	England    

Chivas Regal         	0      	Whisky     	0.4                       	750ml      	Scotland   

Drambuie             	0      	Whisky     	0.4                       	750ml      	Scotland   

Martini & Rossi      	0      	Vermouth   	0.16                      	750ml      	Italy      

The Balverie Carribean Cask 	0      	Rum        	0.43                      	750ml      	Scotland   



HTML Page containing information on my Team:

<html>
<head>
<title>Product Registration</title></head>
<body>
<H1>Spirit List</H1>
<table border=1>
<tr><th>Spirit</th><th>Distributor</th><th>Type</th><th>Alc/Vol</th><th>Size</th><th>Origin</th></tr>
<tr><td>BeefEater</td><td>Pernod Ricardo</td><td>Gin</td><td>0.47</td><td>1litre</td><td>England</td></tr>
<tr><td>Chivas Regal</td><td>Chivas Bros</td><td>Whisky</td><td>0.4</td><td>750ml</td><td>Scotland</td></tr>
<tr><td>Drambuie</td><td>Hiram Walker</td><td>Whisky</td><td>0.4</td><td>750ml</td><td>Scotland</td></tr>
<tr><td>Martini & Rossi</td><td>Barcadi Inc</td><td>Vermouth</td><td>0.16</td><td>750ml</td><td>Italy</td></tr>
<tr><td>The Balverie Carribean Cask</td><td>William Grant</td><td>Rum</td><td>0.43</td><td>750ml</td><td>Scotland</td></tr>
</table>
</body>
</html>


XML file containing information on my Spirits - by Spirit Name ascending:

<?xml version="1.0"?>
<spirits>
 <spirit>
 
 < spiritName>BeefEater</ spiritName>
 < distributor>Pernod Ricardo</ distributor>
 < type>Gin</ type>
 < alcvol>0.47</ alcvol>
 < size>1litre</ size>
 < origin>England</ origin>
 </spirit>
 <spirit>
 
 < spiritName>Chivas Regal</ spiritName>
 < distributor>Chivas Bros</ distributor>
 < type>Whisky</ type>
 < alcvol>0.4</ alcvol>
 < size>750ml</ size>
 < origin>Scotland</ origin>
 </spirit>
 <spirit>
 
 < spiritName>Drambuie</ spiritName>
 < distributor>Hiram Walker</ distributor>
 < type>Whisky</ type>
 < alcvol>0.4</ alcvol>
 < size>750ml</ size>
 < origin>Scotland</ origin>
 </spirit>
 <spirit>
 
 < spiritName>Martini & Rossi</ spiritName>
 < distributor>Barcadi Inc</ distributor>
 < type>Vermouth</ type>
 < alcvol>0.16</ alcvol>
 < size>750ml</ size>
 < origin>Italy</ origin>
 </spirit>
 <spirit>
 
 < spiritName>The Balverie Carribean Cask</ spiritName>
 < distributor>William Grant</ distributor>
 < type>Rum</ type>
 < alcvol>0.43</ alcvol>
 < size>750ml</ size>
 < origin>Scotland</ origin>
 </spirit>
</spirits>