0

I the following array:
enter image description here

And I need to output the values as follows:

"API Docs Portal : op-api-docs"
"Big Ideas : op-bigideas"
"Education : op-education"
....

I've tried something but it doesn't works as expected...

        for (var x = 1; x <= reportValue.GetLength(0); x++)
        {
            for (var y = 1; y <= reportValue.GetLength(1); y++)
            {
                if (reportValue[x, y] != null)
                {
                    var id = reportValue[x, y];
                    var name = reportValue[x, y + 1];
                    var result = name + " : " + id;
                }
            }
        }

Note:
This is how I get the reportValue array (using C# Interop):

    var reportLast = ws.Range["A" + ws.Rows.Count].End(Excel.XlDirection.xlUp).Row;
    var rngReport = (Excel.Range)ws.Range[ws.Cells[2, 1], ws.Cells[reportLast, 2]];
    var reportValue = rngReport.Cells.Value;
12
  • 1
    Are you sure this is a 2d-array? Because usually indices start at 0... Commented Mar 24, 2017 at 14:36
  • 1
    @WillemVanOnsem: Usually, yes - but they certainly don't have to. If you look at the debugger screenshot, this does indeed appear to be 1-based. Commented Mar 24, 2017 at 14:38
  • 3
    " it doesn't works as expected..." doesn't tell us either what you expected, or what happened. Please provide a minimal reproducible example. Commented Mar 24, 2017 at 14:39
  • 1
    Your use of reportValue[x, y + 1] looks pretty suspicious to me... Commented Mar 24, 2017 at 14:40
  • 1
    The 2nd loop is unnecessary, and in fact will read off the end of the array Commented Mar 24, 2017 at 14:40

3 Answers 3

5

It's not clear why you've got two loops here. You always want the same two elements for each row - index 1 and index 2. So you just need:

for (var x = 1; x <= reportValue.GetLength(0); x++)
{
    var id = reportValue[x, 1];
    var name = reportValue[x, 2];
    var result = name + " : " + id;
    // Use result
}

That will fetch [1, 1] : [1, 2], then [2, 1] : [2, 2], then [3, 1] : [3, 2] etc - which looks like it's exactly what you need.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this is exactly what I need! It seems that the array is 1d...is this correct?
There are 2 dimensions to the array, but the "2nd dimension" is merely a key-value pair, it has a fixed length of 2. So you only need to iterate over the the "1st dimension" which seems to be of variable length... Not sure that makes sense to anyone besides myself...
0

Best way for transform 2D array to string in this format is:

var results = string.Join(Environment.NewLine, array.Select(x => string.Join(" : ", x)));

For this array as example:

var array = new string[][]
{
    new string[] { "Hello world", "Hello" },
    new string[] { "Stack", "Overflow" }
};

Output for 'results' is:

Hello world : Hello

Stack : Overflow

Comments

0

The title is somewhat misleading as it doesn't appear to be a standard 2D array.
Nevertheless:

StringBuilder builder = new StringBuilder();
for (int i = 1; i <= reportValue.GetLength(0); i++)
{
    string id = (string)reportValue[i, 1];
    string name = (string)reportValue[i, 2];
    builder.AppendLine(id + " : " + name);
}
// Process the result

This begins a for loop at 1 as the array appears to be 1-based as opposed to 0-based.
Assuming that there is only two values that you require, you can use [i, 1] and [i, 2] rather than looping through the length of the secondary part using GetLength(1).

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.