fork download
  1. # Q6 Perl Assignment Hash of Hashes
  2. # Jeffrey Borgard
  3. # List of Bestselling Books using a Hash of Hashes
  4. # Information about the five books selected from Question 4
  5.  
  6.  
  7. # array:
  8. @books = ("15th Affair", "Me Before You", "The Rainbow Comes and Goes", "Alexander Hamilton", "A Court of Mist
  9.  
  10. and Fury");
  11.  
  12. # and the following Hash of Hashes:
  13.  
  14. %bestsellers = ( "15th Affair" => { Author => "James Patterson",
  15. bookcategory => "Hardcover Fiction",
  16. cover => "Hard",
  17. price => "$16.80",
  18. },
  19. "Me Before You" => { Author => "Jojo Moyes",
  20. bookcategory => "Trade Paperback
  21.  
  22. Fiction",
  23. cover => "Paperback",
  24. price => "$6.47",
  25. },
  26. "The Rainbow Comes and Goes" => { Author => "Gloria Vanderbilt & Anderson
  27.  
  28. Cooper",
  29. bookcategory => "Hardcover NonFiction",
  30. cover => "Hard",
  31. price => "$6.47",
  32. },
  33. "Alexander Hamilton" => { Author => "Ron Chernow",
  34. bookcategory => "Hardcover NonFiction",
  35. cover => "Paperback",
  36. price => "$11.90",
  37. },
  38. "A Court of Mist and Fury" => { Author => "Sarah J.Maas",
  39. bookcategory => "Young Adult",
  40. cover => "Hard",
  41. price => "$11.64",
  42. },
  43. );
  44.  
  45. # To print out best seller books information in the Hash of Hashes (ascending order):
  46.  
  47. print ("\n\nBest selling books - sorted by books:\n\n");
  48.  
  49. printf("%-20s \t%-6s \t%-10s \t%-25s \t%-10 \n", "Books", "Author","bookcategory", "cover","price");
  50.  
  51. @sortedKeys = sort (@books);
  52.  
  53. for $bookname (@sortedKeys) {
  54. $author = $bestsellers{$bookname}{Author};
  55. $bookcategory = $bestsellers{$bookname}{bookcategory};
  56. $cover = $bestsellers{$bookname}{cover};
  57. $price = $bestsellers{$bookname}{price};
  58.  
  59. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-10s \n", $bookname, $author, $bookcategory, $cover,$price);
  60. print "\n";
  61. }
  62.  
  63. # To print out sorted Book information in the Hash of Hashes (descending order):
  64.  
  65. print ("\n\Best selling books - Book Name decending:\n\n");
  66.  
  67. printf("%-20s \t%-6s \t%-10s \t%-25s \t%-10s \n", "Books", "Author", "Bookcategory", "Cover", "Price");
  68.  
  69. @reverseKeys = reverse (@sortedKeys);
  70.  
  71. for $bookname (@reverseKeys) {
  72. $author = $bestsellers{$bookname}{Author};
  73. $bookcategory = $bestsellers{$bookname}{bookcategory};
  74. $cover = $bestsellers{$bookname}{cover};
  75. $price = $bestsellers{$bookname}{price};
  76.  
  77. printf("%-20s \t%-6i \t%-10s \t%-25s \t%-10s \n", $bookname, $author, $bookcategory, $cover,$price);
  78. print "\n";
  79. }
  80.  
Success #stdin #stdout 0s 6044KB
stdin
Standard input is empty
stdout

Best selling books - sorted by books:

Books                	Author 	bookcategory 	cover                     	%-10 
15th Affair          	0      	Hardcover Fiction 	Hard                      	.80        

A Court of Mist 

and Fury 	0      	           	                          	           

Alexander Hamilton   	0      	Hardcover NonFiction 	Paperback                 	.90        

Me Before You        	0      	Trade Paperback 

Fiction 	Paperback                 	.47        

The Rainbow Comes and Goes 	0      	Hardcover NonFiction 	Hard                      	.47        


Best selling books - Book Name decending:

Books                	Author 	Bookcategory 	Cover                     	Price      
The Rainbow Comes and Goes 	0      	Hardcover NonFiction 	Hard                      	.47        

Me Before You        	0      	Trade Paperback 

Fiction 	Paperback                 	.47        

Alexander Hamilton   	0      	Hardcover NonFiction 	Paperback                 	.90        

A Court of Mist 

and Fury 	0      	           	                          	           

15th Affair          	0      	Hardcover Fiction 	Hard                      	.80