Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Arrays confusion

Posted on 2006-07-04
Medium Priority
194 Views
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
Question by:Donelson
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 34

Accepted Solution

Aneesh Chopra earned 1500 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

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

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

Author Comment

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

ID: 17048946
sorry,
no other way....
0

Author Comment

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

## Featured Post

Question has a verified solution.

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

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this articâ€¦
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't hâ€¦
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
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.
###### Suggested Courses
Course of the Month12 days, 4 hours left to enroll