0

HI this my code, am unable to get the length of the array.

package com.mypro {


    public class MyEve {

        public var process:Array ;


        public function MyEve() {
            // constructor code
            this.process = new Array();
            trace("this.process " + this.process)
            this.process["CONFIG_XML_LOADING"]          = 'Configuration XML Loading';
            this.process['CONFIG_XML_LOADED']           = 'Configuration XML Loaded';


            trace("this.process +++ " + this.process.length)

        }

    }
}

OutPut

// Trace
this.process 
this.process +++ 0
4
  • 3
    You shouldn't use an Array as a HashMap. Use a dictionnary. Commented Mar 14, 2011 at 14:37
  • 1
    The resulting 0 is giving you the length of the array. Commented Mar 14, 2011 at 14:37
  • but this is an associate array. the key is CONFIG_XML_LOADED Commented Mar 14, 2011 at 14:43
  • 2
    The actionscript Array class is not intended to be used that way, as others have said. You should look into using a dictionary, or read up on using objects for associative arrays. Commented Mar 14, 2011 at 15:00

3 Answers 3

4

While it's possible to use an array as a associative array, the length property won't work - it'll just return 0. You need to calculate the length yourself using either a for..in or a for each:

for..in:

var len:int = 0;
for ( var key:String in this.process )
    len++;

for each:

var len:int = 0;
for each( var value:String in this.process )
    len++;

as pointed out before though, it's probably better if you use Object (String key) or Dictionary (Object key) for this sort of thing.

Sign up to request clarification or add additional context in comments.

1 Comment

note, you can't really get the length of a dictionary either. So, you'll still have to loop over the keys (or values) to count them. In my code, I have a "sizeOfDictionary(Dictionary)" function that does just that
1

You should use an Object to create an associative array.

public var process:Object;
public function MyEve() {
    // constructor code
    this.process = new Object();
    trace("this.process " + this.process)
    this.process["CONFIG_XML_LOADING"]          = 'Configuration XML Loading';
    this.process['CONFIG_XML_LOADED']           = 'Configuration XML Loaded';


    trace("this.process +++ " + this.process.length)

}

Read this for more information.


EDIT: Ah, yes. As divillysausages pointed out you were trying to get the length. That isn't directly possible for Objects. You can, however, iterate through an Object's properties with the for...in loop, making it trivial to count each property of the object. Here and here are good examples of how to use it.

1 Comment

without getting pedantic, his question was on how to find the length of the array, and this doesn't solve it. I agree than an Object is best to use in this situation though
0

See [] array access from AS3 Reference.

You must use integer value for arrays or instead Array use Object or Dictionary

2 Comments

While it's better to use an Object or a Dictionary, this is a valid use of the Array
@divillysausages Although technically correct, I'm not sure if I'd call this use of the array class valid. Since array extends Object, he is already doing what most have suggested; and the reason 'length' doesn't work is because he isn't using the "Array" specific extension of "Object" to add or remove elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.