If only I were

Building Great Software

PowerShell left hand rule

with one comment

PowerShell uses the “left hand” rule when deciding how to interpret operators. Meaning that the type of the value on the left side of the operator is dominant. Here are some examples:

2+2  (gives 4)

2+”2″  (gives 4)

“2” + 2  (gives “22”, string data type wins out using + for concatenation)

“2” + 5  (gives “25”, concatenation again)

To have a little more fun try some multiplication:

3 * 3 (gives 9, ok this is still boring)

4 * “5”  (gives 20, the “5” becomes an integer)

“3” * 4 (gives “3333”, that’s more interesting, “3” means the string data type is the primary type, this acts much like Ruby)

Here is a bit more that might be interesting:

15,30,45 + 3  (gives a new list of 15,30,45,5)

15,30,45 * 3  (gives a new list of 15,30,45,15,30,45,15,30,45)

Kind of cool how this works.  The DOS prompt was stone-age comparatively.

Advertisements

Written by Chris Sutton

March 27, 2008 at 6:31 pm

Posted in Learning, Technology, Thoughts

Tagged with , , ,

One Response

Subscribe to comments with RSS.

  1. That was informative, thanks. One small correction though: 15,30,45 + 3 (gives a new list of 15,30,45,3)

    Adam Houston

    September 24, 2014 at 12:40 pm


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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: