Year of the Pig Revisited – the magic of QL

Year of the Pig Revisited – the magic of QL

  • Comments 5

Richard Siddaway has a blog entry showing how to calculate the Chinese Horoscope using PowerShell. His solution uses hashtables:

## create hash table
$years = @{1="Rat"; 2="Ox"; 3="Tiger"; 4="Rabbit"; 5="Dragon"; 6="Snake";
           7="Horse"; 8="Goat"; 9="Monkey"; 10="Rooster"; 11="Dog"; 0="Pig"}
## get the year of interest
$y = Read-Host "Enter a year between 1900 and 2100 inclusive"
$anml = ($y - 1899)%12
Write-Host $years[$anml]

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't real comfortable with them yet, you should take a few minutes to experiment and master them. That said, I'm a bad typist so I'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:

PS> function ql {$args}
PS> ql Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog
Pig
Rat
Ox
Tiger
Rabbit
Dragon
Snake
Horse
Goat
Monkey
Rooster
Dog

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'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:

## create hash table
$years = ql Pig Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog
## get the year of interest
$y = Read-Host "Enter a year between 1900 and 2100 inclusive"
$anml = ($y - 1899)%12
Write-Host $years[$anml]

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
  • Just to be awkward, but the Chinese new year does not start in January. I was born in Jan 1969 so I was born at the end of the Year of the Monkey but comes out as a Rooster.

  • Very neat - I'll remember that one

  • An other trick (also saves you from typing the quotes , that I used in my version:

    $cYear = "Rat,Ox,Tiger,Rabbit,Dragon,Snake,Horse,Goat,Monkey,Rooster,Dog,Pig".split(',')

    http://thepowershellguy.com/blogs/posh/archive/2007/02/18/scripting-games-2007-advanced-powershell-event-3-and-4.aspx

    Also, I made a alternate much more powerfull version  using a .NET chinese calendar object :

    http://thepowershellguy.com/blogs/posh/archive/2007/02/18/happy-chinese-new-year.aspx

    Enjoy,

    Greetings /\/\o\/\/

  • I've made some tests, and I became surprised (I don't know if this is the right word in English, but I'll try).

    The QL function is 16x times slower than the common way to create arrays, using the form:

    @('Pig','Rat','Ox')

    I liked because I'm fast using keyboard... but use it in scripts with a lot of array creations isn't a good option.

    Thank you!

    --

    Vinicius Canto <scripterbr_at_gmail_dot_com>

    MVP Visual Developer - Scripting

    MCP Windows 2000 Server, Windows XP e SQL Server 2000

    Blog sobre Scripting: http://viniciuscanto.blogspot.com

  • Vinicius Canto posted a comment regarding the performance of the QL function that I posted in my Year

Page 1 of 1 (5 items)