Hang tight while we fetch the video data and transcripts. This only takes a moment.
Connecting to YouTube player…
Fetching transcript data…
We’ll display the transcript, summary, and all view options as soon as everything loads.
Next steps
Loading transcript tools…
Find and Update - Movie Collection Part 5 - Searching an array and using the array index | SplashKit | YouTubeToText
YouTube Transcript: Find and Update - Movie Collection Part 5 - Searching an array and using the array index
Skip watching entire videos - get the full transcript, search for keywords, and copy with one click.
Share:
Video Transcript
Video Summary
Summary
Core Theme
This content details the development of a movie collection system, focusing on implementing the crucial "find" and "update" functionalities for movies within the collection.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
I'm in the middle of creating a movie
collection system. This movie collection
system will store up to a 100 movies
where each movie has a title, ID, and a
length. We're going to want to be able
to add, find, update, and print movies
in our system. So far, we have the
ability to create individual movies. So,
each individual movie has a title, an
ID, and a length. We have been able to
create a collection. So my collection
has the movies in it and has the size.
So it has a 100 movies, but it keeps
track of how many I actually have in the
system cuz I might not have all of them
in the system at any one time. We have
functions and procedures here to read in
movies and to update and print those
movies. And we've got ones to work with
our collection. I can add a movie into
my collection and I can print movies out
from my collection. Now, what I'm
wanting to do is to be able to find and
update movies. So, in main here, what
I've got is I've got my collection, and
we're starting with the size zero. And
then what I'm doing is adding two movies
into my collection. And at the moment,
printing those movies out. What I want
to do now is be able to find and update
a movie within my collection. And I've
got a bit of a start on that. So, I have
here a procedure where it's going to
perform the find and update for me. So,
it's going to need to find a movie and
then it's going to update it. In order
to do that, it needs to know where the
movies are. So, I have to give it the
collection of movies to work with. But
let's start with find. So, find movie is
going to be used by find and update. And
this could be useful in lots of other
cases. If I want to delete movies or if
I want to do anything else with them,
then I need to be able to find them. So,
to find a movie, if you ask me again, to
find a movie, what do I need? I need to
know where to look. And in this case, I
want to tell the user what what they're
looking for. And so I've written out
that prompt. So this might be uh you
know, update a movie. It might be which
movie do you want to delete. And then
I'm going to ask the user to enter the
ID of the movie. So at the moment, we've
got our collection here. I want to be
able to know which movie you want to
delete by ID. So if you say one, then
I'm update, sorry. If you say one, I'm
going to update this movie. If you say
two, we're going to update this movie.
The find's job is to tell us what the
index is of that movie. So if I enter
27, it should tell me that the index of
that movie is 1. If I update, if I ask
for ID 1, it should tell me the index
for that is zero. And if I enter
anything else, it should tell me that
it's not one of the values in the array.
Now, how could we do that? Well, I could
return anything other than zero or one.
Now if I return a larger number then I
might think that that's actually the
movie to access even though it might be
larger than the number of elements
currently stored. So what's always a
good pattern to use in these cases is
probably to go with minus one. So if I
don't find something inside my uh find
then I know what I can do is return that
minus one. So minus one is a is a good
idea and I can use that to indicate that
it's not found. I did not find it. if I
return minus one. All right. So, movie
collection, how do I find it? I need to
look through all of the
uh elements or all of the movies in my
collection to see which one has the ID
that matches the ID that the user
entered. Now, once again, if we're doing
trying to do something, I'm trying to do
something for all of the elements, I
know that I should think of that as for
each element. And I can use my trusty
for loop to do that. So I can say that
what I know is I know that I want to
start from index zero and I want to loop
while this is less than the size of the
collection and I want to increment I at
the end. So I don't really care how big
the collection is. This will loop over
all of the elements for me. And now I've
got to think what do I need to do for
any one element. So the very first time
I will be zero as we saw before when we
hand executed this. So if I if we've got
I here and I'm executing this uh I will
be zero. I'll then be looking at this
element. So how can I check if the if
this element is the one that I want?
Well, if this element if it tells me
what I need here. If this element is the
one, how do I know? Well, in the
collection I need to go to the movies
at that index. So now I have this movie
and I want to check if that ID is the
same as the ID that the user is after.
Okay. So if this ID is right, so if I'm
after ID 1, then I need to return zero.
Now where is zero? Well, zero is the
value I've got up here in my variable I.
So I has the value zero. So I can return
not zero. I can return I. So I is the
value that would return zero. If we go
through and if it wasn't one then I want
to go to the next element and check that
one. So one thing I know that people
often get wrong here is to do this. Uh
now let's think about what's going to
happen if I do this. So if I do this,
let's check it out. So let's say I do
want ID of 27. So I've got ID as a
variable as well. Let's say the user has
entered 27 into the ID. That's ID there.
There we go. Hopefully you can see that
ID 27. Then we loop through. We get I is
zero. I is less than two. So that's
fine. So I want to access the movie at
at index I. I is zero. Movie at I that's
this one here. Does the ID of that? So
what's the ID of that? The ID is one. Is
1 equal to 27? The answer is no. It's
not. Therefore, I return minus one.
Well, hang on a minute. Hang hang on a
minute. I don't want to return minus one
now, do I? If I return minus one now, I
didn't even bother checking any of the
rest of the collection. I don't I don't
want to do that. So, this else is a
antiattern. You don't want to do that.
If you're doing that, think through
think through it. Think through how it's
working. What I want to do is keep
going. So, now what would happen is we
would get to the end of the loop. I
would add one to I. So I would go to
one. One is still less than two. And I
can access my collection. So collection,
where's my eraser? There we go. Collection
Collection
movies index I. I is one. So this movie
here, does the ID of that movie, what's
the ID? 27. Is 27 equal to 27? The
answer is yes, it is. So that will then
return 27 the value in variable I which
is uh sorry that will then return one
which is the index of the movie which
has the ID 27. So value of I is one. So
we return one telling the caller that
this is the movie that you're looking
for. This is the movie here. The one
with index one is the movie that has the
ID 27. All right. If now we could do the
same thing again if we do that with say
the user enters let me redo that that ID
fear variable. So if let's say I've got
ID here we go so you can see it a bit
better. So if we put the ID of one then
what I do I is initialized to zero here.
Is I less than size? Is 0 less than two?
Yes it is. Then we can check this is the
movie at index I the movie at index I I
is zero at the moment does this movie's
ID is one does one equal one? Yes it
does. Okay I return index zero. So it
stops executing there. So we can see it
works for either of these movies. Now if
the the user entered say 75 as the ID
then index zero that movie doesn't have
ID 75. So we go to one. That movie
doesn't have the ID of 75. So we go to
two. Two is not less than size. So this
remember we check that every single time
we go through the loop. And so that two
is not less than two. So therefore it
would end. And that's when we return
minus one. So I'm returning minus one
because for each if I find one that
matches each one I check does it match
and if it matches I return that index.
If it doesn't match I I don't I
therefore go to the next element. So
performing that for each element. The
only way I can get to here is if by
doing that for each element none of the
elements matched and therefore we got to
the end and now I return minus one.
Okay. So this will now find me the ID of
an element. We can go down here. Let's
give that a try. Um so I can do oops
right line and let's just print it out.
So I'm going to do find movie. So which
movie do I want to find? So let's have a
look. I need to pass in the collection.
So I'll pass in my collection and I'll
pass in the you know test find. That'll
be my that'll be my prompt.
just to see how that works. So save
that. If I come in, compile
and run unused variable index. Ah yes.
Okay. So this is just a warning for the
moment because I haven't finished find
and update. We'll come back and fix that
warning just in a second. And now let's
run that. So movie one ET
All right. So which ID do I want to
find? So if I do 27, when we execute
that, we see we get the movie ID 1,
which is what we expected. That is the
movie at index one. All right. So let's
finish off this very quickly because the
find and update is now going to be super
easy because we've done all the hard
work. Now I know that find movie returns
minus one if it can't find it. So we can
say here it's if index
is less than zero and we could be extra
safe if we really wanted to or index is
larger than or equal to uh the
So if it's less than zero or larger than
the size then I can return from this
because we we did not find it. So in
which case let's write out a message.
Write line no movie
oops to update.
There we go. And we can return.
Otherwise, if it succeeded, then I can
just call our handy update movie
procedure that we wrote earlier and I
can pass in the movie. Which movie?
Well, I know all of the movies are in
the collection in a field called movies.
Now, which one do I want? Well, I want
the one at the index that we found. And
so, this will let me find find a movie.
And now I can update that movie by
accessing it. So if I want to update
this movie, I'm now going to pass a
reference to that movie to the update
movie procedure and it's going to change
the values in memory at this location.
So it's really very nice and convenient
for me. All right, so down here now we
can remove that test. I can put in this
find and then printing it out. Okay,
we're getting very close. Now let's run
compile this and run it. So if we put in
and we could put in different values if
we wanted but this will do. So if I want
to edit the movie at within ID27 that
should be the it movie. So we can see
update it. So I could change the the ID
of that and let's make it 28 instead.
And then maybe I can change the length
to 2.1 and then I'll exit.
And now we can see that that movie was
updated. So by writing these small
procedures and functions, you can very
quickly start to build functionality
once you've got the basic things in
place. All right, I want you to have a
go at that now. So see if you can
implement your own find for whatever it
is you're storing and implement your own
find and update for that thing. And then
you should be able to have this working,
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.