matrixtostrlst
function matrixtostrlst( matrix [, format [, sep]] ) --> strlst
Description
Converts a matrix to a string list where each entry represe_ts a row. The represantation of each entry can be definied with format`. The string between each entry can be definied with _sep, in this case must format also be definied.
Parameters
- matrix
-
The matrix that should be converted.
- format
-
The format from string.format that should be used, by default uses the function %d.
- sep
-
The string that should be inserted between each entry in a row, by default uses the function \t.
Return Values
- strlst
-
The matrix a string list where each entry represents a row from the matrix.
Code
--ZFUNC-matrixtostrlst-v1 local function matrixtostrlst( matrix, format, sep ) --> strlst format = format or "%d" sep = sep or "\t" local strlst = {} for _, row in ipairs( matrix ) do local line = {} for _, val in ipairs( row ) do table.insert( line, string.format( format, val ) ) end table.insert( strlst, table.concat( line, sep ) ) end return strlst end return matrixtostrlst
Examples
local t = require( "taptest" ) local matrixtostrlst = require( "matrixtostrlst" ) local same = require( "same" ) local matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } local strlst = matrixtostrlst( matrix ) t( same( strlst, { "1\t2\t3", "4\t5\t6", "7\t8\t9" } ), true ) strlst = matrixtostrlst( matrix, "%03d", " " ) t( same( strlst, { "001 002 003", "004 005 006", "007 008 009" } ), true ) t()