Solved

Arrays confusion

Posted on 2006-07-04
6
186 Views
Last Modified: 2010-08-05
I have some code --
==================
a = new Array();

b = new Array();
b["Eng"] = {relSize:"+0", txt:"Now is the time"} ;
b["Spa"] = {relSize:"+1", txt:"Ahora es el tiempo"} ;

t = "#main";
a[t] = b;

b = new Array();
b["Eng"] = {relSize:"+0", txt:"for all good men"} ;
a["#2nd"] = b;

trace(a.length);  ---- This returns 0 length !
trace(a[t]["Spa"].relSize + ", " + a[t]["Spa"].txt);
trace(a[t]["Eng"].relSize + ", " + a[t]["Eng"].txt);
trace(a["#2nd"]["Eng"].relSize + ", " + a["#2nd"]["Eng"].txt);
===============

This traces out correctly as --
0
+1, Ahora es el tiempo
+0, Now is the time
+0, for all good men
--------------------------------
EXCEPT: Why am I getting 0 length for the "a" array as noted above?

0
Comment
Question by:Donelson
  • 3
  • 3
6 Comments
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 17039763
Hi,
actual problem here is:

whether you have declared 'a' and 'b' as Array,
but these variables are not anymore holds array

The way you have populated them, you have coverted them to Objects.

To keep them as array, you should populate the values using index

for example:
a[0] = {some value};
a[1] = {some value};
0
 

Author Comment

by:Donelson
ID: 17040721
Aha!  Overlap between the array_index " [ ] " method and the object_index method " [ ] "

So. You can have a two-dimensional object!

Is there any way to get the number of properties on these objects?

Thanks, Aneesh.
0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 17040775
Yes,

there is way to do it:

you can do it using "for(i in Object)" loop

but not, "for(i in Object)" starts from last index to first..


try following code:
-----------
a = new Array();

b = new Array();
b["Eng"] = {relSize:"+0", txt:"Now is the time"} ;
b["Spa"] = {relSize:"+1", txt:"Ahora es el tiempo"} ;

t = "#main";
a[t] = b;

b = new Array();
b["Eng"] = {relSize:"+0", txt:"for all good men"} ;
a["#2nd"] = b;

for ( i in a){
      trace(a[i]["Eng"].relSize + ", " + a[i]["Eng"].txt);
}
-----------

Rgds
Aneesh
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Donelson
ID: 17048916
Aneesh

Yes, thanks, I knew about "for(i in foo)", ...

but is there a way to get the number of elements Without Counting them in a loop?

0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 17048946
sorry,
no other way....
0
 

Author Comment

by:Donelson
ID: 17049945
Thanks Aneesh - nice to speak with you again!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to how to record live broadcast.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question