WEBVTT 00:02.840 --> 00:07.100 Malcolm in his lecture, I'm going to show you how to loop over slices. 00:07.550 --> 00:08.520 All right, let's get. 00:10.130 --> 00:13.430 Do you remember that Arkes is just a slice of strings? 00:15.490 --> 00:17.530 Let's take a look at this in the ED. 00:20.130 --> 00:21.840 First, let's type of follow up. 00:23.000 --> 00:26.510 To keep the program's name, I'm going to initialize it to one. 00:27.900 --> 00:32.820 All right, now, this type of condition like this, I less than learn is that ARC's. 00:33.790 --> 00:35.560 Don Incremented, OK. 00:36.910 --> 00:41.180 As you know, is that ARC's gets the length of the slice, right? 00:41.890 --> 00:48.730 So this will loop for each element inside the slice, it will loop onto the airable becomes less than 00:48.730 --> 00:50.860 the number of elements inside the slice. 00:52.170 --> 00:54.420 OK, now let me print the current element. 00:55.530 --> 00:58.530 Pretty stiff person to new line. 00:59.850 --> 01:07.730 I said, Arkes, I hear notice that I'm getting the element by using a variable instead of a number, 01:07.830 --> 01:08.130 right. 01:08.720 --> 01:11.040 Let me show you let me comment about this. 01:11.040 --> 01:12.460 Called for now, then. 01:12.480 --> 01:14.220 Let's copy this part here. 01:15.870 --> 01:19.320 Then let's type it three times as an example like this. 01:20.890 --> 01:24.640 A novelist changed, I'm able here to a number like this. 01:27.170 --> 01:30.290 OK, now I'm going to run it with three arguments. 01:30.970 --> 01:31.450 Hi. 01:31.460 --> 01:31.970 Hello. 01:32.000 --> 01:32.300 Hey. 01:33.500 --> 01:35.720 As you can see, it prints each argument. 01:37.530 --> 01:38.200 Let's get back. 01:39.480 --> 01:45.210 Now, let's create a variable like this and use it instead as an index expression, it starts from one 01:45.720 --> 01:46.890 for the next item. 01:46.890 --> 01:51.480 Let's increase this very well and let's use it as an index expression. 01:52.490 --> 01:55.140 They do the same for the last item as well. 01:56.140 --> 01:57.160 OK, limerence again. 01:59.920 --> 02:03.450 As you can see, it's prints the same output, right? 02:04.620 --> 02:07.680 So using a number or a variable doesn't matter. 02:09.250 --> 02:10.750 Novelist Lity Scott. 02:12.590 --> 02:14.060 And uncommented the loop. 02:15.150 --> 02:16.220 OK, let's try this again. 02:17.850 --> 02:20.120 As you can see, the output is still the same. 02:21.650 --> 02:24.440 Congress now you learned how to bring the slice. 02:25.540 --> 02:31.200 Now, let's take a look at how to do the same thing for another sliceable that I'm going to create in 02:31.220 --> 02:31.600 a minute. 02:32.890 --> 02:36.220 Now, I'm going to call the fields function from the Springs package. 02:39.570 --> 02:44.230 You know, is the result of the field's function to the words were able, right? 02:45.700 --> 02:51.090 As you can see, fields function takes a string value and returns a slice of strings. 02:51.940 --> 02:58.270 This is the same type of slice like the oyster arc slice that, you know, it will split the string 02:58.390 --> 03:01.870 into its words and then it will return them as a slice. 03:02.110 --> 03:06.910 OK, this allows me to saving the return slice to the words variable. 03:08.010 --> 03:11.940 All right, now let's loop over the sliceable like this. 03:13.820 --> 03:20.000 Here are initialize the variable to zero, because I want to loop over all the items of the word slice. 03:21.390 --> 03:22.820 OK, now let's print it. 03:23.490 --> 03:30.240 Now I'm going to use a formating verb with a minus sign in front of it is minus one here means that 03:30.390 --> 03:35.430 hey, let's print these left aligned is just for pretty printing it. 03:37.930 --> 03:41.410 And since the jury starts from zero here. 03:42.380 --> 03:43.630 I have added one to it. 03:44.670 --> 03:50.210 I'm going to print it like one, two, three, and so not like zero one, two. 03:50.290 --> 03:50.700 OK. 03:51.910 --> 03:57.850 Unless the I'm going to use an index expression to get an item from the word slice using the variable. 03:59.820 --> 04:01.020 All right, limerence. 04:03.410 --> 04:09.670 It prints the items inside the slice by word with their positions inside the sentence. 04:11.460 --> 04:17.460 So if you compare these indexes to the birds inside this ring, you'll see that they match. 04:20.450 --> 04:23.780 So, for example, here, a prince jumps right here. 04:24.740 --> 04:27.620 Let's take a look at the terror threat inside the string. 04:28.770 --> 04:34.130 Here the jobsworth is actually the third word inside this Trink, right? 04:36.560 --> 04:38.360 Let me show you the words visual again. 04:40.380 --> 04:41.040 Here it is. 04:42.170 --> 04:46.090 These vertical lines are the spaces inside these three, right? 04:47.410 --> 04:54.940 So swing state fields function split's this drink by looking at these space characters, then it becomes 04:54.940 --> 04:55.570 like this. 04:58.650 --> 05:04.590 Here, each one becomes a string value and each one is a part of the new slice that is returned from 05:04.590 --> 05:11.140 the strings fields function, and also each one is actually an unnamed rebel, as I said previously. 05:11.790 --> 05:14.940 So you can only access to them by using an index expression. 05:15.830 --> 05:20.320 All right, in the next picture, I'm going to show you a much easier way to loop over strings. 05:20.600 --> 05:21.790 See you there by.