开发者

AS3 trace w/out newline

For diagnostic purposes, I want to print a matrix (an array of arrays). To do this with a loop means executing a trace command multiple times, which—to my understanding—precludes the开发者_StackOverflow中文版 possibility of printing multiple table cells to a single row.

(When coding trace(x); trace(y);, a line break falls between x and y.)

What can I do?


The following code...

var jaggedArray:Array = new Array(new Array(" 1 "," 2 ", " 3 "), new Array(" 3 ", " 4 ", " 5 "));
var output:String = "";
for( var i:Number = 0; i < jaggedArray.length; ++i){
    for(var j:Number = 0; j < jaggedArray[i].length; ++j){
            output += jaggedArray[i][j];
    }
    output += "\n";
}
trace(output);

...produces the following output:

AS3 trace w/out newline

Is this what you're looking for? If it is the case, don't use the ugly string concatenation as I did, it is better to user a buffer as described in the SO question.

A jagged array is is an array whose elements are arrays. It has the peculiarity that its elements can be of different size.


You can use "\n" to break up a trace statement into multiple lines. I'm not quite sure if this is what you were asking in your question.

If your using flex you can use ObjectUtil.toString() to convert an object (nested arrays) into a string representation.

If your asking how to print one line with multiple traces I suggest converting to a string first. imagine you have a matrix broken into a rows array. Note this code isn't tested there may be typos. It also works under the assumption that your outer array contains an array of String objects, this might not be the case but It should help and can be converted for other data types.

var rowText:String = "";
trace ("BEGIN tracing rows");
for each (var row:Array in rows)
{
   for each (var value:String in row)
   {
       rowText = rowText + value + " ";
   }
   trace (rowText);
}
trace("END tracing rows");


var x:int=10, y:int=x/2, z:int=y*2;
// trace as single space-delimited line:
trace(x, y, z); // 10 5 10

but for arrays you can do it like that:

var a1:Array = [1, 2, 35678];
var a2:Array = [124, 5, 6];
var a3:Array = [7, 128, 9];
var mtx:Array = [a1, a2, a3];
for each(var row:Array in mtx) {
    trace(row.join('\t'));
    // or use delimiter function
    // to right align values...
}


To solve this problem, I personally just made a new class which allows me to write to the class; then execute the trace command:

public class TraceHolder {

    private var txt:String = "";

    public function TraceHolder() {
        // constructor code
    }
    public function writeTo(inStrg:String) : void
    {
        txt = txt.concat(inStrg);
    }
    public function execute() : void
    {
        trace(txt);
        txt = "";
    }

}

That Then Allows me to do this:

var th:TraceHolder = new TraceHolder();
th.writeTo("blah blah blah the front of the line");
th.writeTo("blah blah blah");
th.writeTo("blah blah blah end of the line");
th.execute();

Which'd trace this:

blah blah blah the front of the lineblah blah blahblah blah blah end of the line

Simple, effective and darn useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜