-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?

11
  • Is it exactly how your text file is? I mean with { } and stuff Commented Dec 3, 2018 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. Commented Dec 3, 2018 at 15:11
  • Possible duplicate of Convert 2D array to string in C#, looking for most elegant way Commented Dec 3, 2018 at 15:12
  • @Cid yes as is in the text file with brackets and commas Commented Dec 3, 2018 at 15:13
  • 1
    @RandRandom he needs the opposite Commented Dec 3, 2018 at 15:18

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

As the question also your answer - of course - is pretty vague and unspecific.
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 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.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.