Powershell – Introducing ITPros to Developer Terms – Part 3 – Methods – split()

| | Comments (1) | TrackBacks (0)

Powershell

*** WARNING! ***
*** This Blog Post will talk about Developer Terms ***
*** ItPros are cautioned to break out their (Patent Pending) ***
*** “Peril Sensitive Sunglasses” (Ala the HitchHikers’ Guide to the Galaxy) ***
*** Before Reading further. If you are amongst the Bold and the DARING and the ***
*** Truly Devil May Care of ITPros, Please feel free to Read on and live Daring! ***

But don’t say I didn’t warn you :)
Sean – The Energized Tech

 

“When we left our heroes last time…”

Ok let’s remember, this article is to try and get ITPros (non coders) to grasp some of the Methods and techniques that Developers take for granted.   Working with Objects.  Today we’re going to take a quick look at another Method called “split()”

Unfortunately due to a lack of sponsorshop (The other 25 letters and 10 numbers are in the Sunny parts of Floriday relaxing) we have a more “constrained” budget.  So we can’t afford any punctuation marks.

So with that in mind, I’ll introduce you to split()

Split() allows you to to (as it sounds) “Split” strings into chunks an array of objects based upon a common piece.   An example of something we “Split” all the time without thinking about it as a CSV file.   We see a single line of Commas separating the info, we bring it into Excel and we “split” those lines into columns.   So split is something we do all the time.   Only know we’re going to be able to use it on ANYTHING!

“Can we use Split() on this $50? I need change for the parking meter!”

Tap tap tap tap tap…. There’s always a smarty pants in the back.

No but you can use it for is to break information up to manage it if there is a repeatable pattern.   And like other Methods() we find out how to use it with Powershell.

So let’s create a small phrase for fun and store that “Object”

$SillyPhrase=’The Quick Brown Fox Jumped Over the Slow Lazy Dog and Broke His Leg and is now suing the Dog for Interference’

:)

Now again like before all we have to do is right this through a GET-MEMBER to see what Methods and Properties are available on this object like so.

$SillyPhrase | GET-MEMBER

Which will give us a long list of goodies (you can try it in Powershell to see, the One we’re interested in is Split()

image

As before, run that through a “FORMAT-LIST” to read the Definition.

$SillyPhrase | GET-MEMBER –name Split | FORMAT-LIST

To get this definition

image 

****KLUNK!!!**** ***BAM!!!*** ***CRASH!!!*** ***THUD!!!***

Excuse me for a minute?  I need to get some smelling salts for the back row…

Ok, THAT Definition does appear to be like taking apart a car engine, there SEEMS to be a lot to it.  But not if we break it down.  I ‘ll separate the definitions into rows and hopefully that helps you to a) see what it REALLY means and b) see how to separate the different methods of calling up a particular Method.

Yes, some of them have more than one, Some have none.   This one has errr…. Just a few extra.

So this definition which starts out as

string[] Split(Params char[] separator), string[] Split(char[] sep
arator, int count), string[] Split(char[] separator, System.String
SplitOptions options), string[] Split(char[] separator, int count,
System.StringSplitOptions options), string[] Split(string[] separ
ator, System.StringSplitOptions options), string[] Split(string[]
separator, int count, System.StringSplitOptions options)

Now before the back row falls over in a faint, let’s break that down

string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)

Now it really isn’t that bad to deal with.  If you can see, in the definition, each line is just a definition separated by a Comma and preceded by the beginning half of the definition (Type of data being sent in and the Method name)

So wonderful, great.  We have 6, Count ‘em SIX defintions to the Split() method and we haven’t done anything useful with it.  Well I did say it was easy to use didn’t I?

$SillyPhrase.split(“ “)

That’s an example of our first definition.  It’s PARAMeterS are a CHARacter being defined as the SEPARATOR to Split the Object.  We are telling it what our “Delimieter” is between the Characters.  The output of our “Silly Phrase” will be this.

The
Quick
Brown
Fox
Jumped
Over
the
Slow
Lazy
Dog
and
Broke
His
Leg
and
is
now
suing
the
Dog
for
Interference

A bit of a mouthful, but you can see what the “Split()” method does.  It can Split things (Well I guess the name was a little obvious) and the parameters we pass to it govern how the split is done; and in some cases; where.

$SillyPhrase.split(“ “,5)

Which is what the second definition refers states, “Tell where to split on (what Character) and how MANY splits to make (5)” which will give us this result

The
Quick
Brown
Fox
Jumped Over the Slow Lazy Dog and Broke His Leg and is now suing the Dog for Interference

There are other options you can play with on the split that dive right into Developer land, but for the basic use, all the ITPro has to know is “What am I splitting with and do I need to stop"

That’s it, nothing more.  And the crew in the back row can now wake up.   Your snoring just knocked out a ceiling tile. :)

Sean
The Energized Tech

0 TrackBacks

Listed below are links to blogs that reference this entry: Powershell – Introducing ITPros to Developer Terms – Part 3 – Methods – split().

TrackBack URL for this entry: http://www.energizedtech.com/cgi-sys/cgiwrap/jolyrogr/managed-mt/mt-tb.cgi/294

1 Comments

I saw somewhere (can't remember where) definition splitted automaticaly. Maybe something for future post about replace(). It could looks like this:

(($SillyPhrase | Get-Member -Name Split).Definition).Replace('), ',")`n")

David

Leave a comment