{"id":8441,"date":"2007-03-01T08:58:39","date_gmt":"2007-03-01T08:58:39","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/03\/01\/year-of-the-pig-revisited-the-magic-of-ql\/"},"modified":"2019-02-18T13:16:53","modified_gmt":"2019-02-18T20:16:53","slug":"year-of-the-pig-revisited-the-magic-of-ql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/year-of-the-pig-revisited-the-magic-of-ql\/","title":{"rendered":"Year of the Pig Revisited &#8211; the magic of QL"},"content":{"rendered":"<p>Richard Siddaway has a <a href=\"http:\/\/richardsiddaway.spaces.live.com\/Blog\/cns!43CFA46A74CF3E96!266.entry\">blog entry<\/a> showing how to calculate the Chinese Horoscope using PowerShell.  His solution uses hashtables:\n<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier\"><span style=\"color:black;font-size:8pt\">## create hash table<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">$years = @{1=&#8221;Rat&#8221;; 2=&#8221;Ox&#8221;; 3=&#8221;Tiger&#8221;; 4=&#8221;Rabbit&#8221;; 5=&#8221;Dragon&#8221;; 6=&#8221;Snake&#8221;;<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><\/span><span style=\"color:black;font-size:8pt\"><span style=\"font-family:Verdana\">\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-family:Courier\">\n\t\t\t<\/span><span style=\"font-family:Verdana\">\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-family:Courier\">7=&#8221;Horse&#8221;; 8=&#8221;Goat&#8221;; 9=&#8221;Monkey&#8221;; 10=&#8221;Rooster&#8221;; 11=&#8221;Dog&#8221;; 0=&#8221;Pig&#8221;}<\/span><\/span><span style=\"font-family:Courier\"><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">## get the year of interest<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">$y = Read-Host &#8220;Enter a year between 1900 and 2100 inclusive&#8221;<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">$anml = ($y &#8211; 1899)%12<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">Write-Host $years[$anml]<\/span><\/span><span style=\"color:#333333;font-size:8pt\"><span style=\"font-family:Courier\">\n\t\t\t<\/span><span style=\"font-family:Verdana\"><br \/><\/span><\/span>\n\t<\/p>\n<p>This is a perfectly fine solution and a good one to help get hashtables in focus.  Hashtables are super useful in scripting so if you aren&#8217;t real comfortable with them yet, you should take a few minutes to experiment and master them.  That said, I&#8217;m a bad typist so I&#8217;m always looking for ways to do less typing.   I noticed that index for this hash table is a number and that it started at 0.  This immediately made me think of using an array instead.  I then looked at all those quotes and remembered a great trick that Bruce Payette taught me:\n<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier\">PS&gt; function ql {$args}<br \/>PS&gt; ql Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog<br \/>Pig<br \/>Rat<br \/>Ox<br \/>Tiger<br \/>Rabbit<br \/>Dragon<br \/>Snake<br \/>Horse<br \/>Goat<br \/>Monkey<br \/>Rooster<br \/>Dog<br \/>\n\t\t<\/span><\/p>\n<p>What that did was to take a list of arguments (note the lack of quotes) and returns them as an array (actually a stream which gets turned into an array).  You&#8217;d have to go a REALLY long way to find a function with more utility per character than this one!  So now the solution looks like this:\n<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier\"><span style=\"color:black;font-size:8pt\">## create hash table<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">$years = <\/span>ql Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog<br \/><span style=\"color:black;font-size:8pt\">## get the year of interest<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">$y = Read-Host &#8220;Enter a year between 1900 and 2100 inclusive&#8221;<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">$anml = ($y &#8211; 1899)%12<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t\t<br \/><\/span><span style=\"color:black;font-size:8pt\">Write-Host $years[$anml]<\/span><span style=\"color:#333333;font-size:8pt\">\n\t\t\t<\/span><\/span><span style=\"font-family:Verdana;font-size:8pt\"><br \/><\/span>\n\t<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/MMC Architect<br \/>Visit the Windows PowerShell Team blog at:    <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:  <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a>\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Richard Siddaway has a blog entry showing how to calculate the Chinese Horoscope using PowerShell. His solution uses hashtables: ## create hash table $years = @{1=&#8221;Rat&#8221;; 2=&#8221;Ox&#8221;; 3=&#8221;Tiger&#8221;; 4=&#8221;Rabbit&#8221;; 5=&#8221;Dragon&#8221;; 6=&#8221;Snake&#8221;; \u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a07=&#8221;Horse&#8221;; 8=&#8221;Goat&#8221;; 9=&#8221;Monkey&#8221;; 10=&#8221;Rooster&#8221;; 11=&#8221;Dog&#8221;; 0=&#8221;Pig&#8221;} ## get the year of interest $y = Read-Host &#8220;Enter a year between 1900 and 2100 inclusive&#8221; [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Richard Siddaway has a blog entry showing how to calculate the Chinese Horoscope using PowerShell. His solution uses hashtables: ## create hash table $years = @{1=&#8221;Rat&#8221;; 2=&#8221;Ox&#8221;; 3=&#8221;Tiger&#8221;; 4=&#8221;Rabbit&#8221;; 5=&#8221;Dragon&#8221;; 6=&#8221;Snake&#8221;; \u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a07=&#8221;Horse&#8221;; 8=&#8221;Goat&#8221;; 9=&#8221;Monkey&#8221;; 10=&#8221;Rooster&#8221;; 11=&#8221;Dog&#8221;; 0=&#8221;Pig&#8221;} ## get the year of interest $y = Read-Host &#8220;Enter a year between 1900 and 2100 inclusive&#8221; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=8441"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=8441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=8441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=8441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}