ID:1480536
 
Code:
Problem description:
How do you access the length of a matrix without explicitly stating it in the definition?

mob
var
Matrix[][]



Putting empty brackets at the end of the variable name just typecasts it as a /list, it doesn't actually create a list object.

If you want to create a multi-dimensional list, either put the dimensions in the brackets, or use new/list(x,y)
So is there no way to get flexible matrices?

Edit: Experimented with this

#define DEBUG

mob

var
list
Matrix = list(list())

verb
Add2Matrix()
Matrix += 1 ; Matrix[1] += 1
Matrix += 2 ; Matrix[1] += 3
Matrix += 3 ; Matrix[1] += 6

DisplayMatrix()
for(var/i = 1, i < Matrix.len, i ++)
src << "[Matrix[i+1]],[Matrix[1][i]]"

This is the best I could come up with for flexible matrices :/
I'm not sure what you mean by flexible, but a multidimensional list in DM is just a list of lists. If you want to change it's size, you just have to add/remove lists from it and resize the sub-lists.

multi
var/list/data = list()
New(rows, columns)
Resize(rows, columns)
proc
Get(row, column)
return data[row][column]
Set(row, column, value)
data[row][column] = value
Resize(rows, columns)
var/currentRows = data.len
// resize the base list to add/remove rows
data.len = rows
// create a list for each new row to contain the columns
for(var/i = currentRows+1 to rows)
data[i] = new/list(columns)
// update any existing rows to contain the requested number of columns
for(var/i = 1 to min(currentRows, rows))
var/list/L = data[i]
L.len = columns
ToString()
var/string = "{ "
for(var/row = 1 to data.len)
if(row != 1) string += ",\n "
var/list/L = data[row]
string += "{ "
for(var/col = 1 to L.len)
if(col != 1) string += ", "
string += "'[L[col]]'"
string += " }"
string += " }"
return string


<edit>
Changed it to rows,columns instead of columns,rows. Makes it easier to visualize.

You can think of your matrix like this:
//                                     col 1  col 2  col 3
var/list/data = list( /* row 1 */ list("1,1", "1,2", "1,3"), \
/* row 2 */ list("2,1", "2,2", "2,3"), \
/* row 3 */ list("3,1", "3,2", "3,3") )