Solved

Arrays confusion

Posted on 2006-07-04
6
182 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now