Welcome in a small Cotting lecture, I'm going to show you how the multidimensional slices work.
OK, let's get started.
Let's say I want to calculate my spending for the last three days and my total spending.
OK, here is my daily spending.
On the first day I have spent two hundred dollars and one hundred dollars in total, I spent three hundred
dollars and so on.
So how can I represent these data in a multidimensional slice?
Let me show you.
I'm going to short declare a multidimensional slice like so these are my spending on the first day.
And this is my spending on the second day, and lastly, here are my spending for the last day, as
you can see, unlike multidimensional arrays, each inner slice can have a different length.
This is a multidimensional in slice that stores inner in slices.
So each element of the slice is another slice.
OK, now let's calculate my total spending for each day.
To do that, I'm going to range over the spending slice like this, the daily variable here is an inch
slice.
It is one of the inner slices in the spending.
Slice them, show you.
As you can see, each line here is one of the inner slices.
These are my spendings on the first day, on the second day and on the third day.
This calculate my total spending per day to do that, I'm going to declare a variable to hold my total
spending on a particular day.
Remember, the daily variable here is a slice.
It holds my daily spending.
So I'm going to arrange for it to calculate my total spending for a particular day like so.
And lastly, I'm going to add each one of my spendings to the total spending.
And lastly, I'm going to print my total spending LACHSA.
OK, let's run it.
As you can see, it's Prince, my daily total spending so far so good, let's say I want to fetch my
spendings for the last five days from afar or from a database so I can use the make function to create
a multidimensional slice with a length of zero and a capacity of five lachsa.
Remember, when I make a zero length slice, I can append it by beginning from its first element.
It is because the apan function advanced new elements after the length of a slice.
So when I add a new element, it will add it as the first element.
Here are my spandex on the first day.
Here I'm appending an insult to the spending slice is because, as you know, spending is a multidimensional
slice, so it falls in slices.
That is why I can expand the slice to it without using the ellipses operator.
And here are my other Sputnik's.
Check out the last up.
And here I'm opining to insulate us to the spending slice.
I can do so because a spending slice tahseen slices all at the same rate.
As you can see, it has calculated the spending per day howsam.
Now I'm going to show you a more advanced example.
This time I'm going to use a function.
I'll talk about the functions in detail in the later sections, but I think it will be a good introduction.
So first, I'm going to declare a function called the fetch like this.
It returns a multidimensional in Slice Lexar.
A function has its own scope, so I need to create the scope for this function using the Calabrese LACHSA
since the FETCH function returns a slice.
And currently I don't have a slice right now.
So for now, let's return nil value like this.
Remember, I can do so because a slice can be new.
Now, let's get my spandex by calling the function like so.
Here I say the result, value of the fetch function to the spandex variable.
Remember, the fetch function returns are multidimensional in Sless.
So that is why the type of the spandex variable here is also a multidimensional insulin's go in first,
the type of the spandex variable automatically.
OK, limerence.
It doesn't print anything.
It's because the spandex is nil.
So the four range loop doesn't do anything.
OK, cool.
Let's get back to our function.
Remember I told you that I fetched my spandex from a fire, so let's suppose somehow I received this
string value from that fire.
As you can see, each line in the street value contains my day, the spandex.
So each line corresponds to a day.
So how can I load this data into my multidimensional slice?
Well, I need to process it line by line.
Let me show you first.
I'm going to split it into the lines like so.
Remember, the strings that split function splits the given's string value by the given separator and
returns a new string slice.
Let me show you by arranging it LACHSA.
Now, I'm going to print one of the lines, LACHSA.
As you can see, each element of the line slice contains my daily spandex, so I also need to split
each one of those lines.
I need to do so because I'm going to add them to the inner slices one by one.
To do that, I'm going to use a function called the Fields from the Strings Package LACHSA.
The fields function splits the givens shrink value by spaces and returns a new string slice.
Remember, my spending's in a day are separated by a space character.
That is why I'm using the fields function here.
Let me also show you the contents of the field slice by using a range loop like this.
OK, let me print the spandex like so.
OK, limerence, as you can see, the fields function has divided each line like so cool.
Now I'm ready to load them into my multidimensional slice.
So here I'm going to declare a multidimensional slice here.
I'm going to create a slice with the same length of the line.
Slice is because the length of the line slice gives me the total number of days.
Let me show you what the spanning slice looks like.
As you can see, the main function has created a slice with five elements, but each element is a Nield
slice.
Remember, the value of a slice is nil.
Also, remember, the make function is size zero values to slice elements.
So that is why all the slices here have zero values.
So I need to initialize each one of the inner slices here.
I'm going to do that lachsa.
Each element of the spending slice is an inch less, so I'm going to type in slice here and then I'm
going to create the slice with the same length of the field's slice is because the length of the field
slice gives me the total number of my spandex for the particular day.
OK, I'm almost ready to load up the data into the spending slice I divided.
The file contains two its lines, then I divide each one of the lines into my spending, but they are
still string values, so I need to convert them to int values.
To do that, I'm going to use our old friend a Twohy function Lexar.
Here, I skip the error handling, because when there is an error, I will return zero so I don't care
about it.
You you'll see why in a second, OK?
It's time to add this spending to the spending slice.
First, I'm going to get the spending for the current day from the spending slice by using the index
variable.
Then I'm going to get the individual spending amount from that day by using the index variable.
And lastly, I'm going to assign it the current spending like so OK, I'm almost ready.
Let me also remove this print costs.
OK, and now I'm going to return the spending instead of returning Neil here, as you can see, when
you type of value next to a return statement, it returns that value to the calling function.
In our case, the calling function is the main function here.
So the main function receives the spending slice thou calculated in the fetch function.
Then it says it into this spending variable here.
All right, time to run it.
Amala, as you can see, it has calculated my spending for each day.
So let me summarize, you have learned how to use the multidimensional slices and you have also learned
when to use the make function instead of a literal, you need to use it via, you know, the number
of the elements that you're going to add to a slice.
By the way, don't know the content of the elements from the start.
OK, all right.
That's all for now.
If you don't understand some of the parts of this lecture, please bookmark it and then return back
to it after I explain the functions in later sections.
Thank you for watching so far.
See the next lecture by.