0

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 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?

JellicleCat
  • 28,480
  • 24
  • 109
  • 162

4 Answers4

1

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.

Blayzeing
  • 11
  • 1
1

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");
Wes
  • 6,697
  • 6
  • 34
  • 59
  • IF its the last part of this answer you need then please accept jdecuyper's answer. That answer was first and is basically the same. I didn't see it before I continued editing my answer which only included the "\n" part origionally. – Wes Aug 20 '10 at 22:30
1

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:

alt text

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.

Community
  • 1
  • 1
jdecuyper
  • 3,934
  • 9
  • 39
  • 51
1
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...
}
alxzaur
  • 81
  • 3