Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

associative array

Skip to first unread message

techy2001

unread,
May 18, 2006, 4:14:31 PM5/18/06
to
hi,
i have some simple questions-
1.what is the advantage of using Object constructor to create an associative
array over using an Array constructor to create an associative array??
2.What is difference between using a dot over array access syntax .whats the
advantage of one over other??basically where to use which one.
3.how can i find the length of an associative array

thanks
waiting for answers


David Stiller

unread,
May 18, 2006, 4:24:28 PM5/18/06
to
techy2001,

> i have some simple questions-

How do you know they're simple? ;)

> 1.what is the advantage of using Object constructor
> to create an associative array over using an Array
> constructor to create an associative array??

The Array class extends the Object class, so ultimately, an instance of
either class is an Object instance. I don't know that there's really an
advantage to using one over the other. If you instantiate an Object object
and add properties to it, you conceptually have an "associative array," but
you certainly don't have an Array instance -- so you don't have the
Array.length property; you don't have any of the Array class methods, etc.
If you instantiate an Array object and populate it with Object objects,
you'll have access to the Array class members. Only you will know if those
are benefits or not. ;)

> 2.What is difference between using a dot over array
> access syntax .whats the advantage of one over other??
> basically where to use which one.

Dot syntax may be used when you know the name of the property in
question. The array access operator may be used when you need to "drill
down" to a property dynamically, because, for example, you wish to refer to
dozens of sequentially named properties. The array access operator, [],
simply resolves strings to object references.

See this article for details on using the array access operator in this
scenario.

http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically

> 3.how can i find the length of an associative array

If your "associative array" is an Array instance, you reference its
Array.length property. If your "associative array" is simply an object of
objects, you would have to cycle through it with, say, a for..in statement
and count the properties yourself.


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."


Francis Cheng

unread,
May 18, 2006, 7:21:24 PM5/18/06
to
There are two advantages to using an Object instance for an associative array.
First, you can use an object literal to define the associative array. For
example:

var foo:Object = {prop1: "value1", prop2: "value2"};

Second, it avoids any unexpected behavior resulting from the mixture of
indexed and associative arrays. For example, the following code provides
unexpected results because it mixes the two concepts:

var arr:Array = new Array();
arr.foo = "foo";
arr.bar = "bar";
arr.push("foobar");
trace(arr.length); // output: 1

The length property only considers elements in an indexed array (e.g. arr[0]
), and ignores any elements that are attached as properties (e.g. "foo" and
"bar").

It's better not to mix the two types of arrays, so I recommend that you use
the Object class for associative arrays, and the Array class for indexed arrays.

Jeckyl

unread,
May 18, 2006, 7:28:45 PM5/18/06
to
You've made some major errors David !! :)

>> I don't know that there's really an advantage to using one over the
>> other.

The disadvantage is that if you use the Array class for your associative
then you may well think that you can use Array methods on it .. you can't.
You can only use the methods defined in Object class

> If you instantiate an Array object and populate it with Object objects,
> you'll have access to the Array class members.

No you won't ... and that is EXACTLY why you shouldn't do it .. because if
David Stiller can be confused bout this, what hope do mere mortals have !!
:)

>> 2.What is difference between using a dot over array
>> access syntax .whats the advantage of one over other??
>> basically where to use which one.

There is no advantage to either method except that the [ ] array syntax lets
you do things that the dot method doesn't (eg use expresions as varaible
names and use names that do mot match the usual rules for variables)

If you can do it with '.' dot syntax, then there is no advantage or
disadvatnage to the [ ] array syntax as they compile to identical code

>> 3.how can i find the length of an associative array
>
> If your "associative array" is an Array instance, you reference its
> Array.length property.

NO !!!!!!!!!!!!!!!!!!!! you CANNOT use the length property at all.

You HAVE to iterate thru (using a for/in loop) to get the length
--
Jeckyl


David Stiller

unread,
May 19, 2006, 9:30:54 AM5/19/06
to
Jeckyl,

> You've made some major errors David !! :)

Bring it on! :-p

>> If you instantiate an Array object and populate it with
>> Object objects, you'll have access to the Array class
>> members.

> No you won't ... and that is EXACTLY why you shouldn't
> do it .. because if David Stiller can be confused bout this,
> what hope do mere mortals have !!

LOL Dude, I am constantly in awe of your Flash prowess -- have been for
years. It is I, dear fellow, who am the mortal. I'm still trying to make
sense out of ActionScript (and programming in general), and loving every
minute of it. It's a good point you bring up, though. I'm glad you mention
it, because I see now what I've done to muddy the waters.

To my incorrect thinking, an associative array could be represented like
this:

var aa:Array = new Array();
aa.push({tick:"tick"});
aa.push({tack:"tack"});
aa.push({toe:"toe"});

... in which case, of course, the Array.length property holds. The more I
thunk on it, though, the more I realized the above isn't an associative
array at all. It's just an indexed array with object elements. That's
where I went wrong.

Consulting the ActionScript 2.0 Language Reference (I'm a fan), I see
"Note: There is no advantage to using the Array constructor to create an
associative array. The Array constructor is best for creating indexed
arrays" (Creating associative arrays).

In that same section, the instructions for creating an associative array
show exactly what you've mentioned. My apologies to all. :)

And thanks to Jeckyl!

0 new messages