1
00:00:01,240 --> 00:00:04,450
Now, let's look at working with arrays and splitting and

2
00:00:04,450 --> 00:00:08,640
joining them. So I'm going to create a string.

3
00:00:08,640 --> 00:00:15,620
Okay, and just Roy Biv. I can split that string into an array.

4
00:00:15,620 --> 00:00:20,950
All strings have a split method, and this will split on whatever

5
00:00:20,950 --> 00:00:23,960
whitespace is in the string, and then I get an array.

6
00:00:23,960 --> 00:00:29,140
In this case, I get the first name and last name in the array.

7
00:00:29,140 --> 00:00:31,800
However, you could also create a string like this where I've got

8
00:00:31,800 --> 00:00:36,170
"This.Is.A.String" separated by periods. I can split this as

9
00:00:36,170 --> 00:00:41,760
well. In this case, I want to specify, and this is the delimiter

10
00:00:41,760 --> 00:00:44,540
that I'm splitting on.

11
00:00:44,540 --> 00:00:49,740
There are some other kind of fun things that you can do with this split method.

12
00:00:49,740 --> 00:00:57,740
Create another string here. However, I don't want to split on all of the spaces.

13
00:00:57,740 --> 00:01:02,530
I'm going to split on a space, but I only want three elements to the array.

14
00:01:02,530 --> 00:01:06,320
So once PowerShell gets to the third element or last

15
00:01:06,320 --> 00:01:09,540
element, then it will stop splitting.

16
00:01:09,540 --> 00:01:14,000
So I get PowerShell, split, 7, split, and then that's a

17
00:01:14,000 --> 00:01:18,240
count of 3, and so it doesn't split anymore.

18
00:01:18,240 --> 00:01:19,120
However,

19
00:01:19,120 --> 00:01:24,270
that split method is kind of limited because you can only split on a single

20
00:01:24,270 --> 00:01:29,730
delimiter. There's an advanced way to do this with a split operator. So I'm

21
00:01:29,730 --> 00:01:34,420
going to create a string, and this could be the type of string that you might

22
00:01:34,420 --> 00:01:39,210
get from a log file of something or some process that runs and creates a text

23
00:01:39,210 --> 00:01:41,430
file, and you want to parse the data.

24
00:01:41,430 --> 00:01:48,840
So I've got some data string that is separated by numbers, 44, 88, and 435.

25
00:01:48,840 --> 00:01:52,440
And I want to pull that data out using those delimiters. Here's how

26
00:01:52,440 --> 00:01:58,540
I'm going to do that. I'm going to split $r using the split

27
00:01:58,540 --> 00:02:02,300
operator, and I'm going to use a regular expression pattern. Again,

28
00:02:02,300 --> 00:02:03,820
and this is a little advanced.

29
00:02:03,820 --> 00:02:05,410
We haven't talked about regular expressions.

30
00:02:05,410 --> 00:02:08,850
If you understand regular expressions, then this will make sense to

31
00:02:08,850 --> 00:02:14,160
you, but I'm basically telling PowerShell split on a space at least

32
00:02:14,160 --> 00:02:17,140
two digits and then another space.

33
00:02:17,140 --> 00:02:24,040
So what's in $d? I've got four items, and there they are.

34
00:02:24,040 --> 00:02:28,540
So I've got Data foo bar, that first element, then I have a number, or my

35
00:02:28,540 --> 00:02:35,740
delimiter of the space, number, space, and then Field1, and so on.

36
00:02:35,740 --> 00:02:40,480
So that's how I was able to split that string using the split

37
00:02:40,480 --> 00:02:43,340
operator and that regular expression pattern.

38
00:02:43,340 --> 00:02:46,240
If you have things that are a little more complicated,

39
00:02:46,240 --> 00:02:49,840
this is what you're going to need to do.

40
00:02:49,840 --> 00:02:53,950
But that first element, $d[0], could also be treated as an array.

41
00:02:53,950 --> 00:03:00,040
I can split that as well and get those values individually if I needed to.

42
00:03:00,040 --> 00:03:01,590
There's an about topic, again,

43
00:03:01,590 --> 00:03:04,100
you should take a look at. You kind of getting the feeling

44
00:03:04,100 --> 00:03:06,630
I'm urging you to read the help, and there's lots of help

45
00:03:06,630 --> 00:03:10,640
there for you to read? Sure enough.

46
00:03:10,640 --> 00:03:14,440
Alright, so now we need to join things together.

47
00:03:14,440 --> 00:03:21,140
So I'm creating an array, 2, 4, 6, 8, and you can see the values in there.

48
00:03:21,140 --> 00:03:27,030
There is a join operator, ‑join, and then you specify the variable.

49
00:03:27,030 --> 00:03:31,490
Get a result of 2, 4, 6, 8. This looks a little weird, and it's kind

50
00:03:31,490 --> 00:03:34,980
of one of the few operators where you do the operator first and then

51
00:03:34,980 --> 00:03:36,900
the thing that you are working on.

52
00:03:36,900 --> 00:03:43,120
So I'm telling PowerShell, take the array, $r, and join the elements together.

53
00:03:43,120 --> 00:03:47,940
By default, it doesn't include any spaces or delimiters.

54
00:03:47,940 --> 00:03:52,270
You can, however, specify using this syntax,

55
00:03:52,270 --> 00:03:53,890
put the variable first,

56
00:03:53,890 --> 00:03:57,470
then the operator, and then the delimiter that you want to use

57
00:03:57,470 --> 00:04:06,000
such as a space, or maybe you want a dash, whichever you need.

