Archive of October 2009

October 23, 09

AS3 version of PHP's print_r

A very nice little utility nicked from here. Thanks Nate!

package
{
    public function print_r(obj:*, level:int = 0, output:String = ""):*
    {
        var tabs:String = "";
        for (var i:int = 0; i < level; i++, tabs += "\t");

        for (var child:* in obj)
        {
            output += tabs + "["+ child + "] => " + obj[child];

            var childOutput:String = print_r(obj[child], level + 1);
            if (childOutput != "") output += " {\n"+ childOutput + tabs + "}";

            output += "\n";
        }

        if (level > 20) return "";
        else if (level == 0) trace(output); else return output;
    }
}

/* USAGE:

// Save as print_r.as, change package name if needed

var obj:Object = {};
obj.var1 = "test";
obj.var2 = { var2a: "a", var2b: 10 };

print_r(obj);
print_r(("a,b,c").split(","));

*/
09:32 PM | | 0 Comments | Tags: ,