Posted By

mattkenefick on 05/19/08


Tagged

actionscript print printr recurisve


Versions (?)

Print Recursive


 / Published in: ActionScript
 

Recursively trace an array or object in Actionscript.

  1. // To call this function, use "print_r($array);".
  2. //It will return it and trace it. The Print_A
  3. //function is supporting and not to be called on is own.
  4.  
  5. // Minimized
  6. private function print_a( obj, indent ) {
  7. if (indent == null) indent = "";var out = "";for ( var item in obj ) {if(substring(item,0,1)!="_"){var bracks = ['{','}'];var newindent=indent;var isNum = ( !isNaN(Number(item))) ? 'a':'';if(isNum=='a') bracks=['[',']'];if (typeof( obj[item] ) == "object" ){out+= indent+bracks[0] + item + bracks[1]+" => Object\n",newindent = indent + " " ;}else{out+= indent+isNum+bracks[0] + item + bracks[1]+" => " + obj[item]+"\n" ;}out+= print_a( obj[item], newindent+" " ) ;}}return out;
  8. }
  9.  
  10. public function print_r( obj, indent ){
  11. var res = this.print_a(obj,indent);var returner = "Printing " + obj._name + chr(13) + res;trace(returner);return returner;
  12. }
  13.  
  14.  
  15. // Regular
  16. private function print_a( obj, indent ) {
  17. if (indent == null) indent = "";
  18. var out = "";
  19. for ( var item in obj ) {
  20. if(substring(item,0,1)!="_"){
  21. var bracks = ['{','}'];
  22. var newindent=indent; // for indenting obj children
  23. var isNum = ( !isNaN(Number(item))) ? 'a':''; // tells if obj is array or obj
  24. if(isNum=='a') bracks=['[',']']; //changes brackets for arrays
  25.  
  26. if (typeof( obj[item] ) == "object" )
  27. out+= indent+bracks[0] + item + bracks[1]+" => Object\n",newindent = indent + " " ;
  28. else
  29. out+= indent+isNum+bracks[0] + item + bracks[1]+" => " + obj[item]+"\n" ;
  30.  
  31. out+= print_a( obj[item], newindent+" " ) ;
  32. }
  33. }
  34. return out;
  35. }
  36.  
  37. public function print_r( obj, indent ){
  38. var res = this.print_a(obj,indent);
  39. var returner = "Printing " + obj._name + chr(13) + res;
  40. trace(returner)
  41. return returner
  42. }

Report this snippet  

You need to login to post a comment.