Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash trace,dump,print Array variables

Tags:

arrays

flash

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.

like image 499
David King Avatar asked Oct 08 '09 10:10

David King


4 Answers

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]);
like image 123
Amarghosh Avatar answered Nov 02 '22 00:11

Amarghosh


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

... !!! ...

like image 33
user806372 Avatar answered Nov 02 '22 01:11

user806372


Try this:

import mx.utils.ObjectUtil;
trace(ObjectUtil.toString(event));
like image 3
Pete Avatar answered Nov 01 '22 23:11

Pete


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] );
}
like image 2
cleverbit Avatar answered Nov 02 '22 01:11

cleverbit