Solved

Arrays confusion

Posted on 2006-07-04
6
183 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

929 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

14 Experts available now in Live!

Get 1:1 Help Now