WEBVTT 00:01.710 --> 00:07.740 Welcome in a small Cotting lecture, I'm going to show you how the multidimensional slices work. 00:08.370 --> 00:09.540 OK, let's get started. 00:12.130 --> 00:17.230 Let's say I want to calculate my spending for the last three days and my total spending. 00:18.140 --> 00:20.720 OK, here is my daily spending. 00:21.620 --> 00:27.800 On the first day I have spent two hundred dollars and one hundred dollars in total, I spent three hundred 00:27.860 --> 00:28.930 dollars and so on. 00:29.840 --> 00:33.680 So how can I represent these data in a multidimensional slice? 00:33.950 --> 00:34.640 Let me show you. 00:35.070 --> 00:41.030 I'm going to short declare a multidimensional slice like so these are my spending on the first day. 00:41.930 --> 00:48.770 And this is my spending on the second day, and lastly, here are my spending for the last day, as 00:48.770 --> 00:54.230 you can see, unlike multidimensional arrays, each inner slice can have a different length. 00:54.710 --> 00:58.900 This is a multidimensional in slice that stores inner in slices. 00:59.270 --> 01:02.600 So each element of the slice is another slice. 01:03.380 --> 01:06.800 OK, now let's calculate my total spending for each day. 01:08.030 --> 01:14.330 To do that, I'm going to range over the spending slice like this, the daily variable here is an inch 01:14.340 --> 01:14.810 slice. 01:15.350 --> 01:17.810 It is one of the inner slices in the spending. 01:17.810 --> 01:19.730 Slice them, show you. 01:23.520 --> 01:26.950 As you can see, each line here is one of the inner slices. 01:27.690 --> 01:32.460 These are my spendings on the first day, on the second day and on the third day. 01:33.510 --> 01:39.630 This calculate my total spending per day to do that, I'm going to declare a variable to hold my total 01:39.630 --> 01:41.450 spending on a particular day. 01:41.790 --> 01:44.460 Remember, the daily variable here is a slice. 01:44.640 --> 01:46.340 It holds my daily spending. 01:46.710 --> 01:51.540 So I'm going to arrange for it to calculate my total spending for a particular day like so. 01:52.650 --> 01:56.450 And lastly, I'm going to add each one of my spendings to the total spending. 01:57.270 --> 02:00.600 And lastly, I'm going to print my total spending LACHSA. 02:03.530 --> 02:04.630 OK, let's run it. 02:06.250 --> 02:12.850 As you can see, it's Prince, my daily total spending so far so good, let's say I want to fetch my 02:12.850 --> 02:19.780 spendings for the last five days from afar or from a database so I can use the make function to create 02:19.780 --> 02:24.370 a multidimensional slice with a length of zero and a capacity of five lachsa. 02:25.000 --> 02:30.730 Remember, when I make a zero length slice, I can append it by beginning from its first element. 02:31.340 --> 02:35.800 It is because the apan function advanced new elements after the length of a slice. 02:36.490 --> 02:40.330 So when I add a new element, it will add it as the first element. 02:41.080 --> 02:43.030 Here are my spandex on the first day. 02:44.820 --> 02:51.900 Here I'm appending an insult to the spending slice is because, as you know, spending is a multidimensional 02:51.900 --> 02:54.370 slice, so it falls in slices. 02:54.840 --> 02:58.980 That is why I can expand the slice to it without using the ellipses operator. 02:59.730 --> 03:01.700 And here are my other Sputnik's. 03:02.310 --> 03:03.270 Check out the last up. 03:03.270 --> 03:07.640 And here I'm opining to insulate us to the spending slice. 03:08.050 --> 03:13.980 I can do so because a spending slice tahseen slices all at the same rate. 03:14.760 --> 03:18.370 As you can see, it has calculated the spending per day howsam. 03:18.690 --> 03:21.300 Now I'm going to show you a more advanced example. 03:21.510 --> 03:23.560 This time I'm going to use a function. 03:23.580 --> 03:29.520 I'll talk about the functions in detail in the later sections, but I think it will be a good introduction. 03:29.700 --> 03:33.480 So first, I'm going to declare a function called the fetch like this. 03:34.350 --> 03:37.440 It returns a multidimensional in Slice Lexar. 03:38.100 --> 03:45.020 A function has its own scope, so I need to create the scope for this function using the Calabrese LACHSA 03:45.330 --> 03:47.970 since the FETCH function returns a slice. 03:47.970 --> 03:50.480 And currently I don't have a slice right now. 03:50.730 --> 03:53.880 So for now, let's return nil value like this. 03:54.540 --> 03:57.440 Remember, I can do so because a slice can be new. 03:58.560 --> 04:02.610 Now, let's get my spandex by calling the function like so. 04:03.750 --> 04:08.300 Here I say the result, value of the fetch function to the spandex variable. 04:08.820 --> 04:12.890 Remember, the fetch function returns are multidimensional in Sless. 04:13.290 --> 04:20.430 So that is why the type of the spandex variable here is also a multidimensional insulin's go in first, 04:20.430 --> 04:23.070 the type of the spandex variable automatically. 04:23.400 --> 04:24.450 OK, limerence. 04:24.880 --> 04:26.220 It doesn't print anything. 04:26.460 --> 04:28.840 It's because the spandex is nil. 04:29.400 --> 04:31.980 So the four range loop doesn't do anything. 04:32.400 --> 04:33.220 OK, cool. 04:33.270 --> 04:34.770 Let's get back to our function. 04:35.990 --> 04:42.920 Remember I told you that I fetched my spandex from a fire, so let's suppose somehow I received this 04:42.920 --> 04:44.540 string value from that fire. 04:44.840 --> 04:49.240 As you can see, each line in the street value contains my day, the spandex. 04:49.610 --> 04:51.870 So each line corresponds to a day. 04:52.340 --> 04:56.300 So how can I load this data into my multidimensional slice? 04:56.790 --> 04:59.630 Well, I need to process it line by line. 04:59.780 --> 05:01.370 Let me show you first. 05:01.370 --> 05:03.890 I'm going to split it into the lines like so. 05:06.000 --> 05:12.450 Remember, the strings that split function splits the given's string value by the given separator and 05:12.450 --> 05:14.070 returns a new string slice. 05:14.910 --> 05:17.370 Let me show you by arranging it LACHSA. 05:19.630 --> 05:21.730 Now, I'm going to print one of the lines, LACHSA. 05:24.730 --> 05:31.840 As you can see, each element of the line slice contains my daily spandex, so I also need to split 05:31.840 --> 05:33.250 each one of those lines. 05:33.490 --> 05:37.860 I need to do so because I'm going to add them to the inner slices one by one. 05:38.170 --> 05:43.060 To do that, I'm going to use a function called the Fields from the Strings Package LACHSA. 05:43.980 --> 05:49.830 The fields function splits the givens shrink value by spaces and returns a new string slice. 05:49.860 --> 05:54.040 Remember, my spending's in a day are separated by a space character. 05:54.270 --> 05:56.580 That is why I'm using the fields function here. 05:57.370 --> 06:02.400 Let me also show you the contents of the field slice by using a range loop like this. 06:03.960 --> 06:06.240 OK, let me print the spandex like so. 06:08.110 --> 06:14.410 OK, limerence, as you can see, the fields function has divided each line like so cool. 06:14.620 --> 06:17.580 Now I'm ready to load them into my multidimensional slice. 06:18.220 --> 06:21.690 So here I'm going to declare a multidimensional slice here. 06:21.700 --> 06:24.430 I'm going to create a slice with the same length of the line. 06:24.450 --> 06:30.500 Slice is because the length of the line slice gives me the total number of days. 06:30.970 --> 06:33.550 Let me show you what the spanning slice looks like. 06:36.530 --> 06:42.490 As you can see, the main function has created a slice with five elements, but each element is a Nield 06:42.500 --> 06:42.950 slice. 06:43.370 --> 06:46.340 Remember, the value of a slice is nil. 06:46.970 --> 06:51.660 Also, remember, the make function is size zero values to slice elements. 06:51.680 --> 06:55.360 So that is why all the slices here have zero values. 06:55.580 --> 06:58.850 So I need to initialize each one of the inner slices here. 06:58.850 --> 07:00.500 I'm going to do that lachsa. 07:01.400 --> 07:07.310 Each element of the spending slice is an inch less, so I'm going to type in slice here and then I'm 07:07.310 --> 07:13.010 going to create the slice with the same length of the field's slice is because the length of the field 07:13.010 --> 07:17.240 slice gives me the total number of my spandex for the particular day. 07:17.600 --> 07:23.120 OK, I'm almost ready to load up the data into the spending slice I divided. 07:23.120 --> 07:28.760 The file contains two its lines, then I divide each one of the lines into my spending, but they are 07:28.760 --> 07:32.650 still string values, so I need to convert them to int values. 07:32.660 --> 07:36.710 To do that, I'm going to use our old friend a Twohy function Lexar. 07:37.980 --> 07:43.470 Here, I skip the error handling, because when there is an error, I will return zero so I don't care 07:43.470 --> 07:43.850 about it. 07:43.860 --> 07:45.930 You you'll see why in a second, OK? 07:45.930 --> 07:49.370 It's time to add this spending to the spending slice. 07:49.530 --> 07:54.930 First, I'm going to get the spending for the current day from the spending slice by using the index 07:54.930 --> 07:55.320 variable. 07:55.710 --> 08:00.840 Then I'm going to get the individual spending amount from that day by using the index variable. 08:01.080 --> 08:07.140 And lastly, I'm going to assign it the current spending like so OK, I'm almost ready. 08:07.200 --> 08:09.150 Let me also remove this print costs. 08:10.150 --> 08:17.110 OK, and now I'm going to return the spending instead of returning Neil here, as you can see, when 08:17.110 --> 08:22.390 you type of value next to a return statement, it returns that value to the calling function. 08:22.870 --> 08:26.390 In our case, the calling function is the main function here. 08:26.980 --> 08:32.400 So the main function receives the spending slice thou calculated in the fetch function. 08:32.710 --> 08:35.740 Then it says it into this spending variable here. 08:36.610 --> 08:38.020 All right, time to run it. 08:38.740 --> 08:42.850 Amala, as you can see, it has calculated my spending for each day. 08:43.780 --> 08:50.110 So let me summarize, you have learned how to use the multidimensional slices and you have also learned 08:50.110 --> 08:55.960 when to use the make function instead of a literal, you need to use it via, you know, the number 08:55.960 --> 08:58.580 of the elements that you're going to add to a slice. 08:58.600 --> 09:02.180 By the way, don't know the content of the elements from the start. 09:02.570 --> 09:04.120 OK, all right. 09:04.300 --> 09:05.040 That's all for now. 09:05.440 --> 09:11.350 If you don't understand some of the parts of this lecture, please bookmark it and then return back 09:11.350 --> 09:14.780 to it after I explain the functions in later sections. 09:15.310 --> 09:16.500 Thank you for watching so far. 09:16.600 --> 09:17.920 See the next lecture by.