I am trying to write a script that reads an input .txt file that stores a 3D(?) matrix on each line (each line is a different matrix), I have managed to cut the file's lines into separate strings, now I have to read one of the strings (the first line's), which is just a long line of square brackets, commas, and numbers.
//example
[[[[1, []], [6, [1, 2, 11]], [0, [11]]], [[0, [2]], [0, [2]], [0, [11]]], [[0, [1]], [6, []], [0, [11]]]], [[[0, []], [0, []], [0, [11]]], [[0, []], [0, []], [0, [11]]], [[0, [2]], [0, [1,2]], [0, [11]]]], [[[0, [2]], [6, [11]], [0, [11]]], [[0, [6, 4]], [0, []], [0, [11]]], [[0, [1, 2]], [6, []], [0, [2, 11]]]]]
3 columns, 3 rows, and 3 "layers" (there are always 3 layers, but the amount of columns and rows will be different for each file's first line matrix)
the matrix starts from the top left (1x, 1y, 1z) and goes through each 'dimension' backwards (e.g. x1 y1 z1 -> x1 y1 z2 -> x1 y1 z3 -> x1 y2 z1 -> x1 y2 z2, etc),
each cell of the matrix looks like [int, [<comma separated list of ints, nothing if there's no ints>]]
The example above is relatively small compared to some of the other files' first line matrices (the average size of the matrices is 72 x 43 x 3).
And because the matrix's size varies between files, I cannot brute force it.
I can read the matrices by hand, but I have no idea how to tell C# how to read the matrices. I am not very skilled in C# (or coding in general) so I do not know where to start.
CodePudding user response:
You can simply fetch the txt string of the matrix and then de-serialize this test to get your 3d matrix. Sample:
var txtString = "[[[[1, []], [6, [1, 2, 11]], [0, [11]]], [[0, [2]], [0, [2]], [0, [11]]], [[0, [1]], [6, []], [0, [11]]]], [[[0, []], [0, []], [0, [11]]], [[0, []], [0, []], [0, [11]]], [[0, [2]], [0, [1,2]], [0, [11]]]], [[[0, [2]], [6, [11]], [0, [11]]], [[0, [6, 4]], [0, []], [0, [11]]], [[0, [1, 2]], [6, []], [0, [2, 11]]]]]";
var matrix = JsonConvert.DeserializeObject(txtString);
CodePudding user response:
I messed around with something called Regex and was able to get (at least in substring form) all the parts I wanted with this:
/\[[0-9] ,\s\[([0-9]*,?\s*[0-9]*)*\]{1,2}/gi
I probably could have used some kind of JSON array thingamajig, but that would have taken wayyyy too long for me to work with.