# Using associative arrays and for loops together

**URL:** <https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777>\
**Category:** flash\
**Created:** [April 24, 2006, 3:39pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777 "2006-04-24T15:39:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![daffy\_dowden](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/daffy_dowden/32/2371_2.png) [@daffy\_dowden](https://forum.kirupa.com/u/daffy_dowden)\
**Post date:** [April 24, 2006, 3:39pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/1 "2006-04-24T15:39:59Z")

</div>

Hi All,  
Can anyone help me with a query I have regarding associative arrays. Basically, I’m trying to use the current index of a for loop, which is looping through xml attributes, as the key for an associative array.

```auto

1 var attributes:Array = new Array();
2 for (attr in this.childNodes*.childNodes[j].attributes) {
3 //trace(' '+attr+' '+this.childNodes*.childNodes[j].attributes[attr]);
4 attributes[attr] = this.childNodes*.childNodes[j].attributes[attr]; 
5 trace( attr + " - " + atrributes[attr] )
6 }

```

So on line 4 of that code I want the key to be the current xml attribute name. Anyone know how I can do this?

Cheers,  
Daf

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 3:42pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/2 "2006-04-24T15:42:52Z")

</div>

Change line one to make attributes an object

```auto
 var attributes:Object = new Object();

```

If attr is in fact a string, this is all you should need to do.

take care.

\_Michale

---

<div class="post-metadata">

**Author:** ![FizixMan](https://avatars.discourse-cdn.com/v4/letter/f/e9c0ed/32.png) [@FizixMan](https://forum.kirupa.com/u/FizixMan)\
**Post date:** [April 24, 2006, 5:28pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/3 "2006-04-24T17:28:37Z")

</div>

Yup. The Array type can only have numerical entries for keys. However, the Object type works essentially the same way as the array but can use any value for the key. =]

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 5:48pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/4 "2006-04-24T17:48:02Z")

</div>

The reasoning is that the Object class is dynamic, meaning variables and methods can be added to any objects of that class, and the VM will handle memory allocation and such.

So essentially there is no difference, other than clarity between…

```auto

myObj["myVar"] = 5;

// and

myObj.myVar = 5;

```

The nice thing about subscript notation is the fact that you can use variables to create the variables name :), if that makes sense 😛

\_Michael

---

<div class="post-metadata">

**Author:** ![daffy\_dowden](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/daffy_dowden/32/2371_2.png) [@daffy\_dowden](https://forum.kirupa.com/u/daffy_dowden)\
**Post date:** [April 25, 2006, 8:42am UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/5 "2006-04-25T08:42:35Z")

</div>

Cheers Guys!  
Worked perfectly  
:thumb2:

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 25, 2006, 9:06am UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/6 "2006-04-25T09:06:02Z")

</div>

no worries 🙂 GJ

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [April 25, 2006, 1:19pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/7 "2006-04-25T13:19:59Z")

</div>

> [@MichaelxxOA](#):
>
> The reasoning is that the Object class is dynamic, meaning variables and methods can be added to any objects of that class, and the VM will handle memory allocation and such.

Array is dynamic as well, otherwise you’d have to declare an infinite number of properties in the class.

Your real problem is that you spelt attributes wrong in the trace function - but Michael is right, you shouldn’t really use Arrays to store properties other than numerical ones.

🙂

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 25, 2006, 3:28pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/8 "2006-04-25T15:28:34Z")

</div>

Yes, TheCanadian is right. I wasn’t trying to imply that Array was not dynamic, I was explaining why you use an Object. An array here just didn’t make sense. Take Care.

\_Michael

---

<div class="post-metadata">

**Author:** ![daffy\_dowden](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/daffy_dowden/32/2371_2.png) [@daffy\_dowden](https://forum.kirupa.com/u/daffy_dowden)\
**Post date:** [April 25, 2006, 4:55pm UTC](https://forum.kirupa.com/t/using-associative-arrays-and-for-loops-together/186777/9 "2006-04-25T16:55:34Z")

</div>

> [@TheCanadian](#):
>
> Your real problem is that you spelt attributes wrong in the trace function - but Michael is right, you shouldn’t really use Arrays to store properties other than numerical ones.  
> 🙂

Damn typos! they’re the bane of my existance. One of these days I’ll learn to touch type, that’ll show 'em!
