Assosiative Arrays
In this video we look into the usage of objects as associative arrays. We practice this idea with a simple password validation program.
Video Transcript
00:00So, the easiest way to use this bucket, and before we actually could use it, we have to
00:04figure out a way to add content to our object.
00:09And there's a few different ways of adding content, and I want to go through first the
00:12first type, which the first way of adding content in our object is called an associative
00:18array, which basically means it's a key-value combination, which basically works this way.
00:25I would type my variable name, which is O, then I would open a bracket, and notice the
00:31type of bracket.
00:32Uh-huh, it's not curly, it's not round, it's just the straight bracket.
00:39Now I'm going to put a string inside of this area and give a name to that string.
00:44So I'm going to give a value inside of here, which is going to be my key.
00:48And let's say we're going to create a password, a little password application.
00:53So I'm going to put here the username.
00:55First user was going to be called Ben, and then I'm going to close my quote, and then
01:00I'm going to close my bracket, and then I'm going to set an equal, and I'm going to put
01:07a value.
01:08So let's say the password is 3, 4, 7, 4, okay?
01:14So here's our first user, our first element in our object that we've created, which is
01:20an associative array.
01:22And we're going to understand what an associative array is in a future course, but the word
01:26array, what it actually means.
01:28But in an array, the best way to explain it is just a list of things.
01:32And in an object, we create an associated array, because we're associating this key
01:37with this value.
01:38So let's create a few more associative, a few more associations in our associative
01:44array, in our object.
01:47To create another user, we're going to call him Dan.
01:50And Dan would have, let's say 1, 2, 3, 4.
01:55Let's create one more.
01:56We're going to create O, and again, let's give it a name of Bart.
02:03And we're going to give it another value here, which would be 0.
02:06So we created an associative array, we created our object, and our object now has three
02:11different properties, or three different key and values.
02:17Let's see how we could play around with this.
02:19And what I'm going to do is I want to re-leverage our UNT, and our UNT, I'm actually
02:24going to put here a password instead.
02:25So I'm going to change my variable name to make sure that it's more descriptive.
02:30So I'm going to call it pass for password.
02:34And let's leave the password that's in there right now for now, and just create another
02:37variable which would be user, which would be a type of string.
02:42I want to give that string a value of Ben.
02:49Let's see how could we figure out, based on what we've seen so far, how could we connect
02:54between our object and actually test to see if our user and his password are a valid
02:58combination.
03:00The way it works is, and I'm just going to trace it out right here, just for a second.
03:06Let's get rid of the other trace.
03:10So I'm going to trace out O, open bracket, and I'm going to put inside of this
03:15bracket my user.
03:17My user equals Ben.
03:18So it actually would be almost as if I typed Ben.
03:21So I could just type your directly user, and notice that I'm not putting it around any
03:25quotation because I don't want to go to the user that's called user.
03:29If I would quote, if I would double quote it, I would actually be trying to send the
03:33literal string user, which is not the case.
03:36I want to get Ben.
03:37And to get Ben, I actually want to put the variable user inside of my O.
03:43And if I close this up and I just trace it out, what we should see, let's see what
03:49we should see.
03:50We should see the password, which is three, four, seven, four.
03:53Why is that?
03:54Because the user is Ben.
03:56So we're basically going to open, and open equals three, four, seven, four.
04:02So if I run that application right now, we're going to see three, four, seven,
04:06four, and that's exactly what we expected.
04:09Now to make our lives a bit more complicated, we can now ask equals, equals, pass.
04:17So I'm basically asking right here, I'm going to trace out true or false because we're
04:20creating a Boolean right now.
04:23Or even better, let's put here is valid user, going to really use all our different
04:32things that we've learned so far.
04:34And by default, I'm not going to put anything inside because the default is false.
04:40I'm going to grab my is valid user, bring it down underneath my definition, make sure
04:46I wouldn't want to put it up here because my variables were not defined yet, and then
04:49I wouldn't get the values I would expect.
04:53And I'm going to add into the value is valid, just to make it a bit cleaner.
04:58We're going to copy our equation right here and just put it right inside of our
05:04is valid user.
05:05So our is valid user equals whatever the value of this definition is.
05:10And notice that I put a round bracket, I just put it there to make it cleaner.
05:14If I wanted to, I could have put it just like this, but it would be less legible.
05:20By putting these brackets, I'm making it much, much more legible because then it's
05:25very clear that this is one, one side, this is the other.
05:29And you could use round brackets in that way whenever you need to make things a little
05:33bit cleaner while you're building things out.
05:37In other words, it's completely valid, even if you'd want to do something like that.
05:40It wouldn't make it cleaner or just make a mess, but you could, and it would be valid.
05:46So let's trace out our little is valid user message and see if it's really true
05:51or false, which we are assuming now false, right?
05:54Because our user is Ben, that's true, and we're asking if the value inside of OBen or
05:59user, if it equals password, does three, four, seven, four equal 50?
06:07No, it does not.
06:09So if you run our application again, we're going to see the value of false.
06:12So let's make it a little bit nicer.
06:14Let's mix a little bit more things that we've learned already before.
06:18Let's add an if else statement.
06:21So we're going to grab our if, and we're going to grab an else as well.
06:32We could delete this.
06:33So we know if our user is valid, we're going to give you a message of trace you are valid.
06:44Hello.
06:48Hello.
06:48And let's make it even nicer.
06:50We're going to add the actual name of the user, which is user up here.
06:56And in our scenario, it is Ben.
06:59And we could do another trace right here.
07:03I don't know you.
07:12So let's see what I've done here.
07:13We've checked if the user is valid.
07:15If it is valid, then we're going to type, we're going to open to the screen to the
07:19output panel more accurately.
07:20You are valid.
07:21Hello, user, and currently the user is Ben, so it would type out hello, Ben.
07:26If I'm not valid, then it would output I don't know you, Ben, in this scenario.
07:32So let's run our app and see how it, ooh, a little error here.
07:36Let's see what's going on here.
07:37So syntax error, extra characters found after end of program.
07:42So let's double click and see what that means.
07:45And it looks like that we've added here an extra parameter, an extra bracket.
07:50And the easiest way is we've figured this out a time before, we could just mark our
07:55function and use our command quote and our command quote.
08:01And let me just click on F4 to make everything bigger.
08:05So I see that, OK, this function definition looks fine.
08:09When we click on command quote again, this looks OK still.
08:13Click one more time.
08:15And this looks OK as well.
08:18But ooh, we have your one extra that we didn't really mean.
08:21And the reason is probably this extra one right here.
08:23So I'm just going to delete it because we know that we have one too much.
08:27And we could run our application one more time.
08:30And here we go.
08:31I don't know you, Ben.
08:33OK, let's try this now with actually a valid user.
08:36So I'm going to type your user, let's say, Bart.
08:41We're going to get on the password of zero because Bart is zero.
08:46And if we run our app one more time, it will tell us you are valid.
08:50Hello, Bart.
08:53So we've seen now how we were working with an object.
08:55And we've kind of seen a live example of working with an object.
09:00And we've seen how our object is actually a little bit smarter
09:04than what we would expect.
09:07Well, why wouldn't we expect it to be smart, right?
09:10But until now, we worked only with variables
09:13that could contain only one piece of information.
09:17And now we're suddenly working with an object.
09:19And objects could contain multiple types of information.
09:23They could contain multiple things.
09:26They're not limited only to one thing at a time.
09:30Now that we've kind of seen a live sample of working
09:33with an associative array, an object as an associative array,
09:38the next thing that we're going to want to do
09:39is look at an object and see how could we
09:42work with our object as using our dot syntax, which is really
09:48the way most of the time you would
09:50be working with almost every type of object
09:53besides the object type.
