The Surly Admin

Father, husband, IT Pro, cancer survivor

Simple Multiplication Table

My daughter has really been working on her multiplication tables this year and normally she comes home with a study sheet for each number, all the way up to 12.  This week we’re working on 8’s, so 8×0, 8×1, all the way up to 8×12.  Unfortunately she didn’t get a worksheet so she decided to make her own.  If I can take a moment of fatherly pride, she does this kind of thing all the time without us making her.  What a gal!  Well, one of the ways I motivate her is to race her.  So it was on.  Can I write a PowerShell script and get the multiplication table out before she could write it out?!

Loops

This is a classic situation where you’re going to be doing the same thing over and over again, with only slightly different data.  That calls for the single most common structure in all of programming.  The loop.  In this case there are about a dozen different ways of doing this loop.  For/Next, ForEach, Do/While loops, they all work.  Here’s a sample of a PowerShell For/Next loop:


For ($Num = 1; $Num -le 12: $Num ++)
{ #Do stuff
}

view raw

ForNext.ps1

hosted with ❤ by GitHub

I have only used the For loop once or twice in several years, because I find the format really awkward.  And frankly it just looks bad.  One of the cool things PowerShell has is the ability to progress through numbers using a simple “..” format.  So 0..10 would progress a number 0 through 10.  Type it in a PowerShell prompt and see what you get.  In that case you can use the ForEach-Object to make your loop:


1..10 | ForEach { #do stuff with $_ }

With PowerShell 3.0 we can replace using the pipeline object $_ with $PSItem.  This is nice, but neither of those variables really tell you what the variable is, and being a bit of a perfectionist, I like to have more control over my variables.  Also, ForEach-Object isn’t the most performant of techniques out there.  I like using the ForEach statement instead.  I get a slight performance increase and I get to name my variable something that makes sense:


ForEach ($Number in (1..10))
{ #Do stuff
}

If you want to read more about the different loops possible in PowerShell, head on over to PowerShell Pro! and read this.  At this point we have our loop, so all I really need is the base number we’re going to use and the Maximum we want to go up to.  I’ll use Parameters for these numbers so we can change them on the fly.  For my daughter, the maximum is always 12 so I set that as a default so I don’t need to put it in every time.


Param (
[Parameter(Mandatory,HelpMessage="Enter the base for the multiplication table")]
[int]$Base,
[int]$Max = 12
)

After that we just setup our loop, do the calculation and write the output:


ForEach ($Num in (0..$Max))
{ Write-Host "$Base X $Num = $($Base * $Num)"
}

I’ve mentioned it before, but it was buried in another post somewhere so I’ll say it again.  I’m not a big fan of creating variables to just use them once and never again.  If you don’t need to, why do it at all?  With PowerShell we don’t have to. Using parenthesis we can ask PowerShell to execute whatever’s in the parenthesis before passing it on–you may have noticed we did this in the ForEach loop too.  By placing a $ in front of the parenthesis we’re telling PowerShell to convert the results of the expression (whatever’s inside the parenthesis) into a string.  You’ll sometimes have to do this with variables too if you want the value to appear around other text.  We’ll worry about that later.

So in the example above, we use a simple string of “$Base X” (where X is simply an uppercase “x”) and then put an expression next to it that takes whatever the value of $Base is and multiply it by the current number in our loop ($Num) and then write that out with the Write-Host.

Here’s the output:

multiplicationtable

 

Full source code:


Param (
[Parameter(Mandatory,HelpMessage="Enter the base for the multiplication table")]
[int]$Base,
[int]$Max = 12
)
cls
Write-Host
ForEach ($Num in (0..$Max))
{ Write-Host "$Base X $Num = $($Base * $Num)"
}
Write-Host "`n`n`n"

Advertisement

April 4, 2014 - Posted by | Powershell - Getting Started | , , , ,

1 Comment »

  1. Hi admin, i must say you have high quality posts here.

    Your page should go viral. You need initial traffic only.
    How to get it? Search for: Mertiso’s tips go viral

    Comment by 76Edythe | August 7, 2017 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: