print a matrix by 1 to 9 in coldfusion

300 views Asked by At

How to print a matrix in coldfusion like 3*3 matrix, i am new in coldfusion, num = [1,2,3,4,5,6,7,8,9]; this is my array how print this array by 3 rows and 3 columns by programatically . the matrix output is `

1    4    7

2    5    8

3    6    9

` how to create a matrix like this in coldfusion

1

There are 1 answers

3
AndreasRu On BEST ANSWER

We still have no idea about how you have stored your matrix data. Thus, I'm creating a simple example of an approach for storing matrix data and how to output it in HTML. This is just an example about how I'd do it, while there may be other ways.

Storing the matrix data: First you need to have all the data of a matrix stored in such a way that you can easily iterate (loop) over all the values, being able to directly address each value row by row and column by column. A common practice used for that kind of task are Array of Arrays as already commented by @James A Mohler. These are simply arrays stored in arrays!

A code example as cfscript for better explanation:

<cfscript>

    // create empty matrix array to store a matrix
    myMatrix2DArray=[];

    // setting the values row by row with
    // myMatrix2DArray[rowIndex]=[ valueOfColumn1, valueOfColumn2, valueOfColumn3];
    myMatrix2DArray[1]=[1,4,7];
    myMatrix2DArray[2]=[2,5,8];
    myMatrix2DArray[3]=[3,6,9];

    writedump( var=myMatrix2DArray, label="example1" );

</cfscript>

The implicit way of storing the same data:

<cfscript>

    //setting multidimensional array (2D) fully implicitly (example 2)
    myMatrix2DArray=[
        [1,4,7],
        [2,5,8],
        [3,6,9]
    ];

    writedump( var=myMatrix2DArray, label="example2" );

</cfscript>

As you can see from the examples above the matrix stores each row in one array (dimension 1) and an additional array for the column values (dimension 2): thus Array of Arrays - in our example it's a 2D array.

Because you've updated your question providing us with the data as a one dimensional array, I'd turn that into such an 2D array first.

The classic way using a for loop:

<cfscript>    
    //using classic for loop:
    initialArray=[1,4,7,2,5,8,3,6,9];
    row=[];
    myMatrix2DArray=[];
    arrayIndex=1;
    for( element in initialArray ){
        if( arrayIndex mod 3 == 0 ){
            //append last element;
            row.append( element );
            myMatrix2DArray.append( row );
            //reset row and return acc with 1;
            row=[];
            arrayIndex=1;
        }else{
            row.append( element );
            arrayIndex++;
        }
    }
</cfscript>    

The new way using arrayReduce() function (I'm using the member function here):

<cfscript>
   // using arrayReduce() member function
   initialArray=[1,4,7,2,5,8,3,6,9];
   row=[];
   myMatrix2DArray=[];
   initialArray.reduce( 
           ( acc, element ) => {
                   if( acc mod 3 == 0 ){
                       //append last element;
                       row.append( element );
                       myMatrix2DArray.append( row );
                       //reset row and return acc with 1;
                       row=[];
                       return 1;
                   }else{
                       row.append( element );
                       return acc+1;
                   }
           }, 1 );
   writedump( var=myMatrix2DArray, label="example3" );
</cfscript>

Output the matrix data as HTML: Having the data stored as a 2D-Array we can easily loop through all the values to output the data as tabular data in a HTML table as follows:

<cfscript>

    // output style for better view
    writeOutput("<style>table,th,td{border:1px dotted gray;}</style>");


    // output table body and header
    writeOutput("<table><tbody>");
    writeOutput("<tr><th>Column1</th><th>Column2</th><th>Column3</th></tr>");

    for( row in myMatrix2DArray ){

        //open new table row
        writeOutput("<tr>");

        for( column in row ){

            writeOutput("<td>#column#</td>");

        }

        //close table row
        writeOutput("</tr>");
    }

    writeOutput("</tbody></table>");

</cfscript>

The best of this approach is that you can easily access one specific value of the matrix array by simply referencing the row index and the column index with: myMatrix2DArray[row][colum] as follows:

<cfscript>

    writeOutput("<div>Value for Row=2 and Column=3: <strong>#myMatrix2DArray[2][3]#</strong></div>");
    writeOutput("<div>Value for Row=3 and Column=1: <strong>#myMatrix2DArray[3][1]#</strong></div>");

</cfscript>