Welcome in this lecture, I'm going to show you a rarely known feature of Galeras called the Key Elements.
This is also one of the features of slicers, but I'm going to talk about it in the next section, not
here.
OK, sometimes this feature can be useful and only a handful of people are aware of it.
Even the most experienced golfers are unaware of this feature.
So what is a key element?
Let's check it out now.
Let's say you want to create a program that prints the cryptocurrency exchange ratios, for example,
value ask what is the current exchange ratio of each area?
It will display its ratio against the bitcoin.
OK, it will just do a simple conversion.
So to do that, I'm going to declare an array variable.
This will create an area like this one, so according to this area, one yttrium is half of Bitcoin.
OK, the other elements represent different cryptocurrency ratios.
OK, now I'm going to show you the indexes of these elements.
You know this already, but what if I tell you that you can use indexes inside the elementalist of this
area?
Literal, let me show you.
Now, I'm going to type the first element with its index like this and the second element like this
and the last element, this area literal is equal to the previous array, litoral.
So it creates the same array as you can see below.
Let me show you how they work.
These highlighted parts are the key elements.
So this is an area literal.
So each key here corresponds to an index of the array.
As you can see, zero equals two index zero and key one equals to index one.
And the last key two equals to index two and so on.
But this doesn't prove you the real power of the key elements.
Right.
Let's take a look at a better example here.
I have changed the order of the keys.
As you can see, this creates the same as before.
Let's take a look at the indexes again.
The index of the first key element here is one.
So this element becomes the second element of the array.
And the index of the second key element is zero.
So this element becomes the first element of the array.
And lastly, the index of the last element is two, so it becomes the last element of the array, as
you can see.
You can put elements into any index position inside the area by using key elements.
Cool.
Let me show you one more thing, what will happen then use a key element like this.
This will create an area such as this one here, I'm telling God that a six to four area with a length
of three, then I'm saying it to put these elements to the last index, go initializes the first items
to their zero values because it doesn't know their values, because we didn't give them to go.
OK, let's take a look at another example.
What happens if I specify the length using an ellipsis?
Let me show you.
This will create an error like this, as you can see, it creates an error with six elements automatically.
This is because Vango sees that the index of the key element is five.
It thinks that the length of this array should be six.
Let me show you one last thing.
This time will be a more complicated example.
So pay attention.
Look at this area literal here.
I've used key elements and unquit elements together.
Right.
Can you guess what will be the lengths of this error and where Gore will put these elements, pause
the video and think about it for a minute or so, OK?
All right, let me show you go finds the lowest indexed element first, which is this one.
So go first.
Put this element to the first index is because its index is zero.
Dango looks for the second key element, which is this one five.
Since there are no Ankit elements before, that goal will add zero value elements up to five like this,
and then it adds this key element to the fifth index.
And lastly, gold will look for on key elements.
Here is only one on key elements, right?
This one, 2.5.
As you can see, gold puts this element to the index is because an iron key element gets its index from
the last key element.
OK, here, the last key elements index is five.
So these are key elements.
Index becomes six, which is right after the last Ankit element.
Let's take a look at an example in the coating, Ed..
We're going to build a very simple program that will print the cryptocurrency exchange ratios from Array.
First, I'm going to declare an array.
Let's say this array stores to exchange ratios for bitcoin.
So one bitcoin is twenty five that five yttrium and one twenty that five one chain.
These are the names of the cryptocurrency by the way.
All right.
Now let's print them first.
I'm going to print yttrium ratio like this, then I'm going to print one chain ratio.
As you can see here, I'm used to magic numbers, but this is not good.
It is difficult to read.
Right.
So let's refactor this program and let's use the key elements instead.
First, I'm going to declare two constants for yttrium and one change here, I initialize them using
IETA.
If you don't know what Ayata is done, please watch the IETA lecture in the Constants section.
OK, now I'm going to use constants instead of typing the indexes manually.
Let me first change these elements index to constants since this constant equals to zero.
So these elements index also becomes zero.
And next I'm going to define the second elements index as W and W and equals one.
So does the index of this element almost ready now?
Instead of getting the rates by using the magic numbers, I'm going to use my newly declared constants
here as a first.
I'm going to change in zero to H and then I'm going to change one here to w an OK, let's run its cool.
It prints the same output as before.
However, notice that this program is better compared to its previous version because it's more readable.
Right.
Because I'm using very different names like ETA and W and instead of zero and one.
OK, they can print all the elements.
OK, limerence, OK, here, I can change the order of the elements like this limerence again, as you
can see, the output is still the same.
This is because the key elements define the index position here.
The order of the elements that I'm going to type or I have typed here is important.
Let me show you one more example.
Let's say I'm planning to start 10 different currencies here.
To do that, I can just start this constant from nine instead of zero like this.
So the first element will be in the ninth index and the second element will be in this eight index and
so on.
So this will make the area larger.
It's because now the largest index is nine.
Let me show you.
As you can see, all the elements, but last two to have zero values, right?
Let me add one more cryptocurrency here, for example.
Then I need to add it to the area as well like this, let's say its ratio is 20, so I'm going to type
20 here.
Let me run it one more time, as you can see now, ISIS is in the seventh index.
All right, that's all for now, but no worries in the upcoming lectures.
We're going to use the key elements again and again.
So see in the next lecture our.