all groups > flash actionscript > october 2004 >
You're in the

flash actionscript

group:

Named Array Question...



Re: Named Array Question... Patrick Bay
10/1/2004 12:33:53 PM
flash actionscript: Hi,

The differences are numerous. First, an array (true array object)
has a number of methods that allow you to work with its items. An array
has a defined data type and can easily be manipulated. An object is much
more generic and anything you'd want to do with it would require
custom-written functions. Typically, this would involve writing your own
classes.

However, it's interesting to note that an array is simply an
extended instance of the generic Object object :)

Regards,
Patrick

-----------------------------------------------------
www.baynewmedia.com

IRC (www.dal.net) -> #baynewmedia
-----------------------------------------------------



[quoted text, click to view]
Re: Named Array Question... Patrick Bay
10/1/2004 2:15:18 PM
Dan,
Named arrays are the same as standard index-based arrays. Instead of:

myArray[1]="Dan";

You'd used:

myArray['name']= "Dan";

This is the same as addressing an object's property named 'name'.
*However* you can manipulate the array...just have a look at the Array
class for all the methods that are available! It's possible that you
simply weren't accessing your array items correctly.

Regards,
Patrick


-----------------------------------------------------
www.baynewmedia.com

IRC (www.dal.net) -> #baynewmedia
-----------------------------------------------------



[quoted text, click to view]
Named Array Question... Dan
10/1/2004 3:52:13 PM
I have a bit of a question. I'm used to working in Directors Lingo and
am now finally delving into the world of actionscript and have a
question and it is this:

Is there any difference between an array of named elements and an Object
w/ a bunch of properties attached?

Since (for some reason) you loose all ability to actually control the
array using regular array functions what's the point in attaching them
to an array?

Anyways, the answers to this are in no way urgent.. I'm just a bit
confused. Thanks in advance for any light that can be shed on this.

Re: Named Array Question... Patrick Bay
10/1/2004 4:10:07 PM
Dan,

I guess the problem is that you're dealing with abstract data. When
you ask Flash to sort, reverse, or slice the array, it doesn't make any
sense. Sort by what? Slice by what index? If I gave you a list of names
and put them into random containers and then told you to sort those
containers, how would you do it? You could do it alphabetically,
numerically, string length, the order of insertion etc. Of course those
functions won't do anything.

When you refer to an array class that "does work", what do you mean?
What do you expect it to do?

By the way, it's very easy to see the contents of named arrays:

for (item in myArray) {
trace (myArray[item]);
}

Regards,
Patrick

-----------------------------------------------------
www.baynewmedia.com

IRC (www.dal.net) -> #baynewmedia
-----------------------------------------------------



[quoted text, click to view]
Re: Named Array Question... Patrick Bay
10/1/2004 5:14:23 PM
Hmmm...well Dan, I guess ultimately your original point was right! There
isn't a whole whack of difference. *Maybe* there's a difference in the
way that they're allocated in memory but I think only the folks at
Macromedia would know that for sure. In fact, the way that objects can
be accessed as named array elements of other objects, it's very possible
that they're the same thing!


-----------------------------------------------------
www.baynewmedia.com

IRC (www.dal.net) -> #baynewmedia
-----------------------------------------------------



[quoted text, click to view]
Re: Named Array Question... Dan
10/1/2004 5:44:20 PM
I think we're talking about two seperate things here. What I'm talking
about is Flash's NAMED array items. In a named array you can add items
to the array that are linked by name (known as a property list in
director Lingo.)

Such as myArray.name = "dan"

This is indicated as being part of the Array object and yet you cannot
manipulate the item (let alone see it) in your array. The only way to
access it is by calling it as you would a property (ie. trace(myArray.
name);)

The fact that this has NO properties of an array (that I can see) causes
me to think that this is EXACTLY like attaching a property to ANY object
rather than actually creating a proper named array (or property list).

It should show up in the array as well as respond to various list calls.
Right?

Dan

[quoted text, click to view]
Re: Named Array Question... Dan
10/1/2004 7:14:14 PM
patrick,
Here.. this is what I did as a test.

var myArray:Array = new Array(); myArray['name'] = "dan"; myArray['age']
= 26; trace(myArray); trace(myArray).reverse(); trace(myArray).sort();
trace(myArray.age); trace(myArray.name); trace(myArray.slice(1, 2));
trace(myArray.push("Mustache"));
trace(myArray.length);

as you can tell by running it, using any of those array classes does
nothing to the elements of my named array. The push essentially adds an
element to the array. The items are still there but you cannot utilize
the classes making them no better than a Object property. Is there an
array class that DOES work with the named array that i'm missing?
The only thing I've found thus far that lets me work even remotely w/
the named arrays is the sortOn() Method.

Here's a quote from the book Macromedia Flash MX 2004 Professional
Unleashed:
"Naming array elements is an easy way to keep information organized
within an array. None of these named elemnts can be manipulated by
array methods, nor can they be seen when the array is traced."

What then is the point of named arrays as opposed to using object
properties, aside from the sortOn() command?

Cheers,
Re: Named Array Question... Dan
10/1/2004 8:21:08 PM
Patrick,

Yes, I understand HOW to find those names, I'm just asking what the
diffence is between adding named items to an array and just adding them
to an object. You said in a previous statement that there were a whole
bunch of methods available for items in an array. I say that I only can
find one that will manipulate named array items.

As for how you sort them I would assume there would be SOME way. My
suggestion would be that they gain an index number as well as a name.
This is how director differentiates properties in a property list. A
property list would be something like:

pList=[#name="Dan, #age=26, #fortification="vitamins and minerals"]

Any of the properties in the list can accessed via either pList.itemName
or pList[indexNumber]. Obviously this is not the case with Flash, just
an expample. The point being there could be a way to sort them. But
the real question on my mind is again this:

How is a Named Array Element different from an Object Property?

I've so difference (beyond sortOn()... which isn't much) to show me that
the Named Array is really anything. What is it I'm missing? Why would
a named Array be special enough to warrant being called a separate
element when compared w/ the broad object element?

Cheers,
Dan

[quoted text, click to view]
Re: Named Array Question... Dan
10/1/2004 9:38:27 PM
Heh, true and I don't think my simple wondering of *why* really warrants
bugging macromedia about. Perhaps it's a base to be expanded on in
future incarnations of actionscript.

Well thanks for bouncing some ideas back and forth with me. I thought I
was missing something and probably woulda gone nuts pouring through
manuals and sites searching for an answer. Hope I didn't drive you too
nuts. :)

Cheers,
Dan

[quoted text, click to view]
Re: Named Array Question... Rothrock
10/2/2004 2:41:31 PM
And without really knowing much of anything about this, I thought I would chime
in.

The example you gave for director looks an awful lot like XML to me. (Okay
without the opening and closing tags, but you see what I mean.) Perhaps flash's
XML class would be more useful for you? Just a thought.

Oh and one other thing, I'm guessing you are both newsgroup readers? For the
few of us who use the web-based interface it gets really hard to follow the
conversation when you quote the previous messages. It is like, "Haven't I read
this somewhere before, and again, and again.)
Re: Named Array Question... Rothrock
10/2/2004 2:47:21 PM
Here is something I just thought of, and if I get bored at some point I might
try it myself. There might be a difference in the speed of accessing
information.

You could create a hundred (or a thousand) objects.
Attach some value like myObject99.x = 50;
Go through and increment each one.
And time it.

Then create an array.
Do the same thing.
And time it.

Might be interesting. Or not.

Re: Named Array Question... Rothrock
10/2/2004 3:47:19 PM
It would seem that array is faster. I was able to create an array with 100 x
values, add a y value, increment the x value and sort the array in ascending
order by x in the same time that I could create 100 objects and attach an x
value.

I wouldn't even know how to sort the objects. But I'm thinking it won't take
negative time to do so. :)

I also tested the time just to increment the variable (without the overhead of
the creation of either object or array.) Again Array was faster. (I upped the
index to 1000 for this part of the test.)

So I'm thinking, at least on the speed side, Array is the way to go.
AddThis Social Bookmark Button