# associative arrays

## Video: associative arrays

Now we just talked about arrays. And because we just talked about arrays I want to talk to you about something else that's very closely related. And that's something called an Associative Array. When we use an array object then we're using the Array class. And the Array class gives us the opportunity to order our data using integer indexes. Integers are whole numbers. So, 0, 1, 2, 3, 4, 5, et cetera. Now an Associative Array doesn't necessarily have to be created using the Array class.

## associative arrays

Now we just talked about arrays. And because we just talked about arrays I want to talk to you about something else that's very closely related. And that's something called an Associative Array. When we use an array object then we're using the Array class. And the Array class gives us the opportunity to order our data using integer indexes. Integers are whole numbers. So, 0, 1, 2, 3, 4, 5, et cetera. Now an Associative Array doesn't necessarily have to be created using the Array class.

In fact, most of the time we're going to create Associative Arrays using the Object class. That's object with a capital O. Object is the base class - sort of the most fundamental class upon which all other classes are based. Typically, we'll use the Object class to create an Associative Array. What an Associative Array does is it helps you to organize your data. Instead of using an integer index, we use a string index.

And we refer to those string indices as keys. An array created using the Array class like we just talked about in the previous lesson, is useful when you want to create some structure to your data. That way you can sort your data and organize it in lots of different ways. Using an Associative Array is helpful when you want to group some related data, but they don't necessarily have any order. Instead they have particular significances. And what I mean by that is, for example, you might have some information about a company.

And you would have the employees of your company. You might have John, and Sue, and Fred. And each of these people who are employees have different titles. Maybe one's the president and one's the CEO, and one's the CTO. So they don't necessarily have a particular order. But they do have particular significance. And they're all related. So we could use an Associative Array in order to group that information. And then we could give each of the pieces of data a key in order to be able to reference it.

And the key in those cases would be president, or CEO, or CTO. So let's take a look at a code example of this. I'm going to open the Actions panel and I'm going to instantiate an Associative Array. So I am going use the var keyword, of course, because I want to declare a variable in order to store my Associative Array. And I'll call it oEmployees. And I'll define that as being an Object datatype.

And then I am going to use the Object constructor - that's object with a capital O - in a new statement. So I'll type new Object. Now, one thing I want to talk about before we move on is we have the Object with a capital O. And because ActionScript in MX 2004 is case sensitive, then if we were to use a lowercase o, that's not the same thing. When we use the lowercase o, it's just simply a term we use to describe a generic instance of a class.

But the Object with a capital O is a specific class name. And you'll notice that when I type it with a capital O, then I get code syntax coloring, and that indicates that that is a keyword. And that is the Object class. And that's differentiated from object with a lowercase o, which is a general term that we use to describe instances of classes. Whether that be an instance of the Array class or a MovieClip class, or any of the other classes that we'll discuss throughout the remaining lessons of this title.

So in order to create our simple Associative Array, we're creating an instance of the Object class. In order to add data to my Associative Array I am going to use dot syntax. There are two alternative ways that you can do this. And we're going to start with dot syntax. In order to do that, I am going to type the name of my Associative Array. Which is oEmployees. And then I use dot. And then I follow that with the key. And the key is something that I get to make up.

And in this case I'm going call one of them president. And then I use an assignment operator. And I assign to that the value of - let's say that "Sue" is the president of this particular company. And then I can add another element to my Associative Array. And let's say that "John" is the CTO of my company.

And let's add one more. And let's say that "Sarah" is the CEO of this company. So I have created an Associative Array that has three elements. And each element has a key. The keys are president, cto, and ceo. And then I have the values that I have associated with those keys of Sue, John, and Sarah. Now, if I want to be able to retrieve those values, then I can use the dot syntax in a situation such as within a trace statement.

If I wanted to output the value, then I could use, as an example, oEmployees.president. And when I test the movie, I'll see that Sue is displayed in the Output panel. Now there's another way that we can reference the elements of the array. And that is using array access notation. So let's take a look at that, and then we'll compare and contrast the benefits of each of these two techniques.

So using array access notation, we still start with the name of the Associative Array. Which is oEmployees, in this case. And then I use the square brackets in order to specify the key. And then the key is going to be specified as a string. So let's create one more element in our Associative Array. And we'll use the key of "cfo". And let's assign that a value of "Arun".

And then if I want, I can also reference that value, and we could use a trace statement to illustrate this. I can also reference the value using array access notation. So I can type oEmployees and use the square brackets. When I test the movie I will see that Arun is displayed in the Output panel. I can additionally, if I wanted, I could also reference the same element using dot syntax.

So they're interchangeable for the most part. And the same would be true if I wanted to reference the CEO using array access notation. So why would I use one over the other? It's a good question. The benefit that the array access notation has over the dot notation is that the key is specified as a string. And this becomes really valuable when we're wanting to use things that are more dynamic. For example, if our Associative Array has keys that are determined more programmatically, or if we don't know them to hardcode them at the time we're writing our code, then we want to use the array access notation because then we can use a variable as the key.

Here's an example. If I create a variable called sKey and I assign that a value of "ceo", then I can reference the element that corresponds in my Associative Array using the sKey variable inside of the square brackets. In order to get the bigger picture for this, I am going to tell you this. All objects created from all classes in ActionScripts can be treated as Associative Arrays, this becomes particularly valuable when we're working with, for instance, movie clips.

And then what happens is a lot of people often times want to know how they can reference a movie clip dynamically. And what they can do is use Array Access notation in order to reference the movie clip. And we'll get a chance to see some of that in action in the movie clip section of this title. All right. Objects and arrays. They can seem a little bit complex when you're first starting with them. But you can definitely pat yourself on the back; it's an advanced topic, so you're definitely moving along on the ActionScript path.

We're going to be using a lot of these concepts more in the later exercises, so you'll get a chance to practice with them. One thing that I need to tell you, though, is it's important that you just be willing to have an open mind and try these things. There's something to be said for "Slow and steady wins the race." That's true. But you also need to be willing to take the steps. So we're going to be walking through a lot of exercises together. I would encourage you to practice with them. If there's any sticking point, then try it again. And you'll always find the finished and commented versions of all the exercises to use for reference if there's any point that you're uncertain about.

Show transcript

#### This video is part of

ActionScript 2.0 Essential Training

92 video lessons · 29020 viewers

Author

Expand all | Collapse all

46s
1. welcome
46s
2. ### Introducing ActionScript 2.0

7m 54s
1. ActionScript and the environment
1m 55s
2. working in the environment
5m 59s

1h 41m
1. understanding datatypes and variables
12m 28s
2. exercise: variables
6m 49s
3. expressions and operators
6m 4s
4. using if
10m 59s
5. exercise: if
3m 10s
6. using switch
8m 2s
7. exercise: switch
3m 11s
8. using for
12m 6s
9. exercise: for
3m 28s
10. working with functions 1
5m 20s
11. working with functions 2
7m 16s
12. working with functions 3
3m 22s
13. working with functions 4
5m 15s
14. exercise: functions
3m 7s
15. using setInterval
8m 25s
16. exercise: setInterval
2m 41s
4. ### Introducing Objects and Arrays

27m 11s
1. objects
3m 49s
2. arrays
13m 28s
3. associative arrays
9m 54s
5. ### Using Movie Clips and Buttons

49m 58s
1. movie clips and buttons
3m 15s
6m 48s
3. event handler methods
10m 57s
4. basic animation effects
5m 9s
5. exercise: slideshow 1
6m 32s
6. exercise: slideshow 2
4m 59s
7. draggable movie clips
7m 38s
8. performing hit tests
4m 40s
6. ### Adding Movie Clip Content Programmatically

1h 11m
1. introducing attachMovie
13m 28s
2. exercise: windows 1
6m 19s
3. exercise: windows 2
5m 21s
4. exercise: windows 3
3m 51s
5. exercise: windows 4
6m 40s
10m 57s
5m 38s
11m 17s
5m 12s
2m 28s
7. ### Working with Text

2h 26m
1. dynamic text fields
13m 37s
2. exercise: article viewer 1
8m 33s
3. formatting text
10m 20s
4. making HTML formatted text
5m 18s
5m 53s
6. exercise: article viewer 2
6m 23s
7. scrolling text
10m 57s
8. exercise: article viewer 3
8m 32s
11m 38s
6m 4s
11. exercise: article viewer 4
5m 38s
12. making input fields
5m 51s
10m 3s
14. exercise: article viewer 5
20m 55s
7m 41s
16. exercise: article viewer 6
8m 56s
8. ### Creating Basic User Input Forms

52m 39s
1. introducing components
1m 55s
2. the button component
10m 26s
3. combo box and list
9m 25s
4. multiple selections for a list
3m 19s
5. the checkbox component
4m 15s
5m 40s
7. exercise: quiz form 1
10m 12s
8. styles for components
5m 22s
9. exercise: quiz form 2
2m 5s
9. ### Controlling Sound

1h 14m
1. creating and attaching sound
6m 9s
6m 37s
7m 25s
4. start, stop and pause
4m 46s
5. setting the volume
6m 17s
6. playback
5m 39s
7. playback dragging
4m 51s
8. exercise: sound 1
5m 54s
9. exercise: sound 2
5m 12s
10. exercise: sound 3
2m 54s
11. exercise: sound 4
3m 48s
12. exercise: sound 5
5m 15s
13. exercise: sound 6
9m 47s
10. ### Working with Video

57m 58s
1. introduction to video
2m 43s
2. creating a video symbol
6m 27s
3. attaching video to the stage
3m 45s
4. exercise: video 1
9m 9s
5. play and pause
3m 33s
6. exercise: video 2
2m 19s
7. video progress
2m 37s
8. exercise: video 3
3m 37s
9. audio and video
4m 23s
10. exercise: video 4
6m 42s
11. looping playback
4m 52s
12. exercise: video 5
2m 34s
13. exercise: video 6
5m 17s

9s
1. goodbye
9s

### Start learning today

Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

### What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

### Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

How to use exercise files.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Congratulations

You have completed ActionScript 2.0 Essential Training.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

How to use exercise files.

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

• Mark video as unwatched
• Mark all as unwatched
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

#### Get our Annual Premium Membership at our best savings yet.

Thanks for signing up.

We’ll send you a confirmation email shortly.

• new course releases
• general communications
• special notices

Keep up with news, tips, and latest courses with emails from lynda.com.

• new course releases