on 12-Dec-2008 13:42
Well, now that I've got the Networking and Social Media ABC's completed it's time for another round of glossary fun and games. Being from a development background, I figure it's about time to focus on something a little more technical. For a while now, I've been toying with the PowerShell scripting language from Microsoft so I figure that's as good a place as any to get started on a new series. So, in the next 28 work days or so, be on the looking for another ABC listing of all the need goodies in the PowerShell language.
Since there's no time like the present, I'll go ahead and start this series out with a post on the letter "A". What programming/scripting language would be complete without the ability to manipulate numbers. Well, PowerShell allows you to do that with some fun extensions that make life a little bit easier for you. So today's post is Arithmetic Operators.
Unlike strongly typed languages like C/C++ and Java, PowerShell is a dynamically typed language meaning that it can "morph" data types depending on the context in which they are used. This comes in handy when you need to do string manipulations. PowerShell has the standard arithmetic operators you would expect from basic programming language as illustrated in the following table.
| |||
Operator | Description | Example | Result |
---|---|---|---|
| |||
+ | Add two values together | 1+2 "Foo" + "Bar" (1,2,3) + (4,5,6) | 3 "FooBar" (1,2,3,4,5,6) |
* | Multiply two values | 2 * 3 "z" * 5 (1,2) * 2 | 6 "zzzzz" (1,2,1,2) |
- | Subtract one value from another | 5 - 2 | 3 |
/ | Divide two values | 4 / 2 5 / 2 | 2 2.5 |
% | Return the remainder from a division operation | 9 % 5 | 4 |
|
The Addition Operator (+)
PowerShell defines the behavior of the addition operator for numbers, strings, arrays, and hashtables. Adding or multiplying two numbers produces a numeric result. Adding two strings performs a string concatenation resulting in a new string. Adding two arrays behaves like strings in that the two arrays are concatenated into a single new array. The fun stuff happens when you mix operand types. In this situation, the type of the left operand dictates the behavior on how the operation will proceed. Some examples below illustrate how things work
PS> 1 + 2 -> 3
PS> 1 + "2" -> 2
PS> "1" + 2 -> 12
PS> (1,2,3) + 4 -> (1,2,3,4)
PS> "hi " + (3,4,5) -> "hi 3 4 5"
PS> "hi" + 3 -> "hi3"
PS> 3 + "hi" -> Error - Input string was not in the correct format.
The Multiplication Operator (*)
PowerShell defines the behavior for the multiplication operator for numbers, strings, and arrays. The only legal right-hand operator for multiplication is a number. Multiplying numbers works as expected. If the operand on the left is a string, then the string is repeated the number of times specified by the right operand. Similarly, if an array is the left operand, the elements in the array are repeated the number of times specified by the right operand and the new array is returned. Some examples of the multiplication operator are as follows:
PS> 1 * 2 -> 2
PS> "hi" * 2 -> "hihi"
PS> (1,2,3) * 2 -> (1,2,3,1,2,3)
PS> 2 * "hi" -> Error - Input string was not in the correct format.
The Subtraction Operator (-)
The subtraction operator is only defined on numbers and subtracts the right hand operand from the left hand operand. Unfortunately there aren't any fun extensions for strings or arrays.
PS> 2 - 1 -> 1
PS> 2.5 - 1 -> 1.5
The Division Operator (/)
The division operator, like the subtraction operator, only is defined for numbers. The returned value is the number of times the right hand operand can be divided into the left hand operand.
PS> 2 / 1 -> 2
PS> 5 / 2 -> 2.5
The Modulus Operator (%)
The modulus operator only works on numbers as well and returns the remainder from a division operation of the right to the left operand.
PS> 5 % 2 -> 1
PS> 11 % 6 -> 5
Now you've got all the tools you need to master your arithmetic in PowerShell!