is there a way to trace an ARRAY in FLASH.
I want to have an output similar to PHPs command:print_r(myArray)
for ex: (in flash):
var event:Array = new Array();
event['name']='david';
trace(event); // that display anything
while print_r(event) in PHP would display as string:
Array {
['name'] => david,
}
I want to achieve same kind of result in flash.
trace(array.join());
would work for numerically indexed arrays. For associative arrays, you have to use for..in
construct.
for(var t:Object in array)
trace(t + " : " + array[t]);
Actionscript trace function (in any actionscript language versions) is pretty much a big shame.
Just try that to laugh:
var a :Array = [1,2,3];
var b :Array = [4,5,6, a];
a[3] = b;
trace(a);
A non shameful trace function should indeed loop over the array's elements and trace the arrays inside the arrays as the AS3 trace function does. But it should also check for circular references between inner and parent arrays inside the root array which is traced. This can be implemented in a recursive or iterative way.
If you're not agree that AS3 trace function is bad then also consider the fact that this function will not let you see if an array is contained inside another. I mean that this code:
var a :Array = [1,2,3];
var b :Array = [a, 4,5,6];
trace(b);
will output this:
1,2,3,4,5,6
although we could expect this kind of output:
[1,2,3],4,5,6
And finally if you have null or undefined values inside your arrays then they'll be traced as empty strings:
var a :Array = [1,2,undefined,3];
var b :Array = [4,5,6, null, a];
trace(b);
will output this:
4,5,6,,1,2,,3
... !!! ...
Try this:
import mx.utils.ObjectUtil;
trace(ObjectUtil.toString(event));
Have you tried using the .toString
method on Array
?
trace( myArray.toString() );
Here are the docs reference:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#toString()
Alternatively if you are using associative arrays, then the way to trace its values would be:
for (var prop:String in myArray)
{
trace( prop, " = ", myArray[prop] );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With