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
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:
The implicit way of storing the same data:
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:
The new way using
arrayReduce()
function (I'm using the member function here):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:
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: