Welcome back.
Previously, you had learned how to create the lock parser program in this lecture, you're going to
learn how to enhance it using tracks.
Let's get started.
In this lecture, most of the time, I'll be moving to variables into Azraq, and I love fixing the
errors so that you understand how to add a track to an existing project.
OK, let's start by declaring a strike type.
I'm going to name it parser.
This track will store details about the log parsing.
For now.
Just leave it here.
Now, let's take a look at these variables.
I'm going to add them to the track as fields so that in the upcoming sections it will be easier to work
on this project.
First, I'm going to cut the first variable from here.
Now I'm going to pass it into the truck.
Of course, there are errors.
Let's fix them one by one.
The first error is about the same variable.
It hasn't been declared yet.
Let's create a new instance of the parsers track.
So now I'm going to move the make function into the struct literal.
I just need to change the equals sign to a column here to make function returns be a new map and I use
it to initialize the Sonfield.
OK, now let's take a look at the next error.
It says the sum is not defined easy to fix.
Let's type that in front of it.
This fixes the error because there is a sonfield in the parser struck.
The next error is the same.
Let's fix it in the same way.
Now let's take a look at the next error.
Again, the same error.
Let's do the same thing also in here.
All right.
No more errors.
The program works.
Good.
Let's go up.
Let's move the first variable into the strike type.
Let's fix the errors.
Let's quickly select all the domain variables.
Now I'm going to get them from the parser instead, the program still works good.
Let's go up again.
Now, let's move the first variable into this truck.
Time to fix errors.
I'm going to put that in front of the variable to get it from the strike value.
Let's also get this one from the strike value as well.
Good, I didn't break anything.
Let's go up one last time and move the last variable into the structure.
I'm going to select all the variables and I'm going to get them from the struck again.
Cool, it works.
So far, so good.
The variables were declared in the main function, skop, now they're in this truck, so as a side benefit,
they won't clutter the namespace of the main function anymore.
So now it's clear that they are related because they are in the same truck.
There is a good proverb.
It says, make the zero value useful here except the Matfield.
I make the zero value useful here.
As you can see, I do not need to initialize the rest of the fields as they are fine with their zero
values.
Now let's take a look at the log file.
You know, the log file contains the domain name and the number of visits for that domain.
Let's also represent this concept in the code right above the track.
I'm going to declare another one.
Now I'm going to add to fields.
Domain name is a string widgets.
As in the results, track will stored visits for a domain in the feature.
You can add more metrics in it if you need to, such as unique visits.
And so, as you can see, it is better to represent concepts using extracts because they allow us to
make our programs more flexible and extendible.
You might ask why I didn't use a map instead.
Well, I could.
However, a shrug is more obvious.
You can see its fields directly.
If I was using a map, you wouldn't know anything about its content without printing it.
For now, I'll continue using a track.
OK, now, instead of tracking the number of visits using an integer, let's track it using the results
track.
Of course there are errors.
Now let's fix the first one.
I'm going to change the element type of the map to result.
OK, here comes the interesting part, because the next error is more subtle.
It says you cannot increase the results track.
You know, this court returns a result value.
Let's take a look at the fields.
Let's select the visits fields.
However, it doesn't fix the error.
But why?
Well, you can only change and addressable value, but the map element is not an addressable value.
I'll talk more about this in the Pointers section later, by the way.
So what is the solution?
Well, it is quite simple, actually.
You just need to create a new copy of the results and assign it to the map.
Remember, the copy contains the last line from the log file.
OK, let's get the total number of visits for the domain so far, and I'm going to add the total number
of visits to the copy.
OK, lastly, I'm going to assign the copy to the map.
So now the map element contains the total number of visits for the domain.
It brings the truck values instead of the total number of visits.
So let's go the first I'm going to change the name of this variable to make its intentions clear, next,
I'm going to print the total number of visits per domain by getting it from the strike.
So.
Now, it works excellent, by the way, I can type this code more concisely.
I'm going to assign it to the map directly.
It works.
All right, that's all for now.
See in the next picture.