ahmed
August 17, 2003, 6:55pm
1
ahmed = []
ahmed["a"] = "sdf"
ahmed["b"] = "asd"
trace(ahmed.length) // outputs 0
ahmed = []
ahmed[0] = "sdf"
ahmed[1] = "asd"
trace(ahmed.length) // outputs 2
I got two questions:
a) Why does the first piece trace 0 instead of 2?
b) How would i loop through an array where the indexes are literal (not numbers)
any help would be appreciated
system
August 17, 2003, 7:03pm
2
arrays (lengths) are number based. :-\
system
August 17, 2003, 7:11pm
3
Ahmed, in the first case you are using associative array.
ahmed = []
ahmed["a"] = "sdf"
ahmed["b"] = "asd"
trace(ahmed["a"]) // outputs sdf
I think you cannot retrieve its length since associative array isnt based on index numbers.
system
August 17, 2003, 7:13pm
4
I think he’s trying to do enumeration.
system
August 17, 2003, 7:14pm
5
[AS]
ahmed = [{a:“sdf”,c:“whatever”},{b:“afd”}]
trace(ahmed[0].a) // outputs sdf
trace(ahmed[0].c) // outputs whatever
trace(ahmed[1].b) // outputs afd
trace(ahmed.length) // outputs 2
ahmed = []
ahmed[“a”] = "sdf"
ahmed[“b”] = "asd"
trace(ahmed[“a”]) // outputs sdf
ahmed = []
ahmed[0] = "sdf"
ahmed[1] = "asd"
trace(ahmed.length) // outputs 2
[/AS]
I didn’t manage to get the length of the array using “a” and “b”, since arrays are number based, but if you want to use letters at all costs, you could use the above using objects. This is basically similar to nested arrays, only I’m using objects.
1)What sen said
What sen is about to say :P:P
system
August 17, 2003, 7:20pm
6
if you want, you could always make your own method for checking that
Object.prototype.addProperty("propertyNum",function(){
var L = 0;
for (var i in this) L += this.hasOwnProperty(i);
return L;
},null);
ahmed = []
ahmed["a"] = "sdf";
ahmed["b"] = "asd";
ahmed[0] = "sdf";
ahmed[1] = "asd";
trace(ahmed.length) // outputs 2
trace(ahmed.propertyNum) // outputs 4
system
August 17, 2003, 7:52pm
7
thanks for the proto sen
I just realized that I can for…in-loop through the associative array, that i think solves it