-3

I am trying to add values to a 2 dimensional array of int[,]

The text file ConnectedCaves contains the following data

0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0

I need to have it in the following format;

int[,] graph = new int[,] 
{
    { 0,0,0,1,0,0,0 },
    { 0,0,0,1,1,0,0 },
    { 0,0,0,0,1,1,1 },
    { 1,0,0,0,1,1,0 },
    { 0,1,1,1,0,0,0 },
    { 0,0,1,1,0,0,0 },
    { 0,0,1,0,0,0,0 }
};

I haven't been able to find much on how this is done. and there fore don't have any code that would be of use to share.

Is it possible to add this as it appears in the text file to the 2 dimensional array? If not directly what would be the best approach to getting this data into the array in the format show in the text file?

bdg
  • 465
  • 6
  • 18
  • Is it exactly how your text file is? I mean with `{ }` and stuff – Cid Dec 03 '18 at 15:09
  • Without you showing what you´ve tried so far it´s imposible to help you, because we simply don´t know what exactly your problem is. – MakePeaceGreatAgain Dec 03 '18 at 15:11
  • Possible duplicate of [Convert 2D array to string in C#, looking for most elegant way](https://stackoverflow.com/questions/46630565/convert-2d-array-to-string-in-c-looking-for-most-elegant-way) – Rand Random Dec 03 '18 at 15:12
  • @Cid yes as is in the text file with brackets and commas – bdg Dec 03 '18 at 15:13
  • @bdg How did you tried to parse it? – Renatas M. Dec 03 '18 at 15:16
  • 1
    @RandRandom he needs the opposite – Cid Dec 03 '18 at 15:18
  • @Reniuz I havent had much luck parsing the data, ive amended the input data to see if it makes it easier to get the data into the `int[,]` – bdg Dec 03 '18 at 15:38
  • It's almost a Json. If you replace some char and add others. ok Only the comma should stay but almost – xdtTransform Dec 03 '18 at 15:43
  • Wait what? The file format just change? What is the file data? There is no more brace.. If it's one line how do we know the size of the array? – xdtTransform Dec 03 '18 at 16:02
  • @xdtTransform sorry for the confusion the data can be in either the format that you have in you're answer or above, i had set the data formatted as the graph should look as i thought it may have been easier to add it in that format. – bdg Dec 03 '18 at 16:06
  • Well I have a question.. For this new input format with everything on the same line how do you know how many item will be in your sub list? Does the sub array Always have 7 elements? For this structre I will advice searching for "[Split a List into smaller lists of N size](https://stackoverflow.com/questions/11463734)" – xdtTransform Feb 28 '19 at 09:27

3 Answers3

1

It's almost a Json:

Array definition
(source: json.org)

From this we know that the final structure of your string must be like :

[
  [ 0,0,0,1,0,0,0 ],
  [ 0,0,0,1,1,0,0 ]
]

So with simple replace, and using Json.net to deserialize the json:

var inputs =
@"{ 0,0,0,1,0,0,0 },
{ 0,0,0,1,1,0,0 }";

var jsoned = String.Format("[{0}]", inputs.Replace('{', '[').Replace('}', ']'));
var result = JsonConvert.DeserializeObject<int[,]>(jsoned);
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
xdtTransform
  • 1,986
  • 14
  • 34
0

You would need code to:

  1. Split that input into rows.
  2. Split each row into cells, fields or seperate values
  3. Put those values into an array

Step 1+2 can be done with REGEX, I asume. It those curly brackets were not there, any CSV parsing code might also be able to handle it.

Step 3 will propably require parsing. You generally do not want to use strings or char in your code. This looks like ints, so parse them into ints and work with those.

As for the backend array: If you do not know how many lines there will be - or even how many fields per line - you have to use List or another advanced collection instead. And for those you can only use the jagged style, afaik.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • As the question also your answer - of course - is pretty vague and unspecific. – MakePeaceGreatAgain Dec 03 '18 at 15:22
  • ok so for example if i removed all of the brackets and commas, has the data in a list of ints. could that then be split into the rows shown in the question and curly brackets and commas applied as needed? – bdg Dec 03 '18 at 15:23
  • @bdg The current input can be split already. That is what Regular Expressions exist for. It would jsut be slightly easier if it followed all the limitations for CSV files, so you could sick any random CSV parser onto it. RegEx is a bit more involved. – Christopher Dec 03 '18 at 15:28
0

Using Split, Trim and Select you can easly have a List<int[]>.

var inputs =
@"{ 0,0,0,1,0,0,0 },
{ 0,0,0,1,1,0,0 },";

var lines = inputs.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

var dim =
    lines.Select(
        line =>
        line.Trim(new[] {'{', '}', ','})
            .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
            .Select(element => int.Parse(element))
            .ToArray()
    ).ToList();

Then you can use Jon's answer to How to convert list of arrays into a multidimensional array

static T[,] CreateRectangularArray<T>(IList<T[]> arrays)
{
    // TODO: Validation and special-casing for arrays.Count == 0
    int minorLength = arrays[0].Length;
    T[,] ret = new T[arrays.Count, minorLength];
    for (int i = 0; i < arrays.Count; i++)
    {
        var array = arrays[i];
        if (array.Length != minorLength)
        {
            throw new ArgumentException
                ("All arrays must be the same length");
        }
        for (int j = 0; j < minorLength; j++)
        {
            ret[i, j] = array[j];
        }
    }
    return ret;
}
xdtTransform
  • 1,986
  • 14
  • 34