Actionscript 3 equivalent of PHP's print_r

Here's a handy little function for easing the development process with Actionscript 3 in Flex and Flash: an equivalent of PHP's recursive print function print_r, which displays objects and arrays in a way that's readable by humans.

Code for Flex:

  1. import mx.controls.Alert;
  2.  
  3. public function pr(obj:*, level:int = 0, output:String = ""):* {
  4.     var tabs:String = "";
  5.     for(var i:int = 0; i < level; i++, tabs += "\t");
  6.     
  7.     for(var child:* in obj){
  8.         output += tabs +"["+ child +"] => "+ obj[child];
  9.         
  10.         var childOutput:String = pr(obj[child], level+1);
  11.         if(childOutput != '') output += ' {\n'+ childOutput + tabs +'}';
  12.         
  13.         output += "\n";
  14.     }
  15.     
  16.     if(level == 0) Alert.show(output);
  17.     else return output;
  18. }
  19.  
  20. public function test():void {
  21.     var obj:Object = new Object();
  22.     obj.var1 = "test";
  23.     obj.var2 = { var2a: "a", var2b: 10 }
  24.     
  25.     pr(obj);
  26.     pr([ "a", "b", "c" ]);
  27. }

Code for Flash:

  1. function pr(obj:*, level:int = 0, output:String = ""):* {
  2.     var tabs:String = "";
  3.     for(var i:int = 0; i < level; i++, tabs += "\t");
  4.     
  5.     for(var child:* in obj){
  6.         output += tabs +"["+ child +"] => "+ obj[child];
  7.         
  8.         var childOutput:String = pr(obj[child], level+1);
  9.         if(childOutput != '') output += ' {\n'+ childOutput + tabs +'}';
  10.         
  11.         output += "\n";
  12.     }
  13.     
  14.     if(level == 0) trace(output);
  15.     else return output;
  16. }
  17.  
  18. //Test
  19. var obj:Object = new Object();
  20. obj.var1 = "test";
  21. obj.var2 = { var2a: "a", var2b: 10 }
  22.  
  23. pr(obj);
  24. pr([ "a", "b", "c" ]);

Both tests will display:

  1. [var1] => test
  2. [var2] => [object Object] {
  3.     [var2b] => 10
  4.     [var2a] => a
  5. }
  6.  
  7. [0] => a
  8. [1] => b
  9. [2] => c
Author rcz Time Oct 14, 2008 Comment on 'Actionscript 3 equivalent of PHP's print_r' Add comment Bookmark 'Actionscript 3 equivalent of PHP's print_r' Bookmark Permalink to 'Actionscript 3 equivalent of PHP's print_r' Permalink
Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Netvouz Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Facebook Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Furl Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Technorati Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Stumble Upon Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Digg Bookmark 'Actionscript 3 equivalent of PHP's print_r' to Del.icio.us

Comments

No comments posted yet.

Add comment

Your name
Your email or website (optional)
Icon
Confirmation code
Confirmation code
Message
face:happy face:glasses face:devilgrin face:crying face:grin face:laughing face:sad face:kiss face:shocked face:wink face:monkey face:angel