Looping through array

Can someone help me with the logic of looping through an array.
I would like to identifying like strings in a specific location [i,2] and add the associated values in [i,3] and save the results into a new array or list.

Graphically trying to do this.

Example
Thanks for any assistance.

Regards,
Jed-
JedidiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Jedidia;

In your example how do you differentiate between the AAA's at the top of the array and the AAA's at the bottom of the array because the List at the right has two AAA's group?
JedidiaAuthor Commented:
You can't differentiate them other than their location in the array.
The only way I can think of doing this is to grab the first row in the array and test this value against the next in a loop?
NorieAnalyst Assistant Commented:
Jed

How are you currently storing the first array?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

JedidiaAuthor Commented:
Sorting the array as follows
string[,] updatedData = new string[((lineCount) / 2), 38];

There is alot of other data that is not important to the new list/array.
Kelvin McDanielSr. DeveloperCommented:
If I'm understanding what you're trying to do correctly, I would use the constructs in the .NET Framework that make this type of thing easier. Specifically, I would use a Dictionary<string, double> to make the summing by label much easier to code and understand exactly what is happening. Going this route makes things trivially simple:

var result = new Dictionary<string, double>();

for (var i = 0; i < data.Length / 2; i++)
{
	double value;
	var key = data[i, 0];
	double.TryParse(data[i, 1], out value);

	if (result.ContainsKey(key))
	{
		result[key] += value;
	}
	else
	{
		result.Add(key, value);
	}
}

Open in new window

Here's a working fiddle where you can see it in action and make real-time adjustments to suit your needs.
louisfrCommented:
Using a Dictionary won't work because the same key can appear several times.
Here is a method that groups the labels by proximity.
        static IEnumerable<(string, double)> Sum(this string[,] array)
        {
            string key = null;
            double sum = 0;
            int i;
            for (i = 0; i < array.GetLength(0); i++)
            {
                if (i != 0 && key != array[i, 0])
                    yield return (key, sum);

                double.TryParse(array[i, 1], out var value);
                if (key == array[i, 0])
                {
                    sum += value;
                }
                else
                {
                    key = array[i, 0];
                    sum = 0;
                }
            }
            if (i != 0)
                yield return (key, sum);
        }

Open in new window

Kelvin McDanielSr. DeveloperCommented:
@louisfr In this case the dictionary will absolutely work. The key check in line 9 below prevents it from being duplicated.
Did you actually look at the working  dotnetfiddle that was provided in the answer?

var result = new Dictionary<string, double>();

for (var i = 0; i < data.Length / 2; i++)
{
	double value;
	var key = data[i, 0];
	double.TryParse(data[i, 1], out value);

	if (result.ContainsKey(key))
	{
		result[key] += value;
	}
	else
	{
		result.Add(key, value);
	}
}

Open in new window

louisfrCommented:
I didn't need to test it to know it doesn't get the expected result.
Since a dictionary can't have the same key twice, you're adding the 3 first lines from the example with the 3 last lines.
Your result:
AAA: 259.5
BBB : 8913.4
CCC: 26

Open in new window

Expected result:
AAA: 38.5
BBB : 8913.4
CCC: 26
AAA: 221

Open in new window

Kelvin McDanielSr. DeveloperCommented:
@Jed, is there a difference between the top 'AAA' and the bottom 'AAA', or are they the same label?
JedidiaAuthor Commented:
@Kelvin, Yes, your solution combines all the like values into one record like the below.

AAA: 259.5
BBB : 8913.4
CCC: 26

I have not been able to get @louisfr's suggestion to work (but will try again later tonight or tomorrow morning).
kaufmed   ( ͡° ͜ʖ ͡°)*whispers*  I C# people.Commented:
If you install morelinq from NuGet into your project, then you can use the GroupAdjacent function:

using MoreLinq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp14
{
    class Program
    {
        static void Main(string[] args)
        {
            var array = new List<KeyValuePair<string, double>>()
                        {
                            new KeyValuePair<string, double>("AAA", 1.8),
                            new KeyValuePair<string, double>("AAA", 25.7),
                            new KeyValuePair<string, double>("AAA", 11),
                            new KeyValuePair<string, double>("BBB", .4),
                            new KeyValuePair<string, double>("BBB", 44),
                            new KeyValuePair<string, double>("BBB", 8846),
                            new KeyValuePair<string, double>("BBB", 23),
                            new KeyValuePair<string, double>("CCC", 15),
                            new KeyValuePair<string, double>("CCC", 11),
                            new KeyValuePair<string, double>("AAA", 135),
                            new KeyValuePair<string, double>("AAA", 31),
                            new KeyValuePair<string, double>("AAA", 55),
                        };

            var grouped = array.GroupAdjacent(item => item.Key);

            foreach (var group in grouped)
            {
                Console.WriteLine("{0} => {1}", group.Key, group.Sum(item => item.Value));
            }

            Console.ReadKey();
        }
    }
}

Open in new window


Screenshot

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JedidiaAuthor Commented:
Thank you all.

I was able to get my code working based off the kaufmed example.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
arrays

From novice to tech pro — start learning today.