Search code examples
c#.netdictionaryforeachkey-value

How to loop through multiple dictionaries to obtain KVP?


I currently have 20 Dictionary<string, Vector3> that are storing TimeStamp Key and Vector3 Value.

E.g.

Dictionary<string, Vector3> rWrist = new Dictionary<string, Vector3>();
Dictionary<string, Vector3> rThumbProximal = new Dictionary<string, Vector3>();
Dictionary<string, Vector3> rThumbDistal = new Dictionary<string, Vector3>();
Dictionary<string, Vector3> rThumbTip = new Dictionary<string, Vector3>();
Dictionary<string, Vector3> rIndexKnuckle = new Dictionary<string, Vector3>();

On exit, I am attempting to loop through each dictionary to generate a CSV with TimeStamp and X,Y,Z coordinates.

I was successful in generating a one to two CSVs manually.

E.g.

foreach (KeyValuePair<string, Vector3> kvp in rWrist)
{
  writer.WriteLine("{0},{1},{2},{3}", kvp.Key, kvp.Value.x, kvp.Value.y, kvp.Value.z);
}

But... to do this manually for all 20 dictionaries would be a pain. I am pretty lost on how I could iterate through each dictionary at once.

E.g.

for (int i = 0; i < paths.Count; i++)
    {
        if (!File.Exists(paths[i]))
        {
            File.WriteAllText(paths[i], null);
        }

        using (var writer = new StreamWriter(paths[i]))
        {
            writer.WriteLine("{0},{1},{2},{3}", "Time", "xPos", "yPos", "zPos");
            
                foreach (KeyValuePair<string, Vector3> kvp in LOOP-THROUGH-MULTIPLE-DICTIONARIES-HERE)
                {
                    writer.WriteLine("{0},{1},{2},{3}", kvp.Key, kvp.Value.x, kvp.Value.y, kvp.Value.z);
                }

        }
    }

I'm not a software developer by trade so any help would be greatly appreciated!

Edit for Clarity: I am using HoloLens2 to poll positional data every tick

Using the internal clock - each tick is stored as a Key and the value is assigned the Vector3 position of that joint at that tick

Each dictionary may or may not have the same TimeStamps if HoloLens2 doesn't detect a finger pose that tick.

I need to export different .CSV for each joint for Data Analysis


Solution

  • I need to export different .CSV for each joint for Data Analysis

    From my understanding, that requirement contradicts your previous statement:

    I am pretty lost on how I could iterate through each dictionary at once.

    , but if you need to export the joint data to separate .CSV files, I would suggest something similar to the following:

    var vectorDataByJointName = new Dictionary<string, Dictionary<string, Vector3>>
    {
        [nameof(rWrist)] = rWrist,
        [nameof(rThumbProximal)] = rThumbProximal,
        [nameof(rThumbDistal)] = rThumbDistal,
        [nameof(rThumbTip)] = rThumbTip,
        [nameof(rIndexKnuckle)] = rIndexKnuckle,
        // and so on for the remaining joints
    };
    
    foreach (var jointVectorData in vectorDataByJointName)
    {
        // File creation here (using jointVectorData.Key as file name?)
    
        writer.WriteLine("{0},{1},{2},{3}", "Time", "xPos", "yPos", "zPos");
    
        foreach (var kvp in jointVectorData.Value)
        {
            writer.WriteLine("{0},{1},{2},{3}", kvp.Key, kvp.Value.x, kvp.Value.y, kvp.Value.z);
        }
    }
    

    (nameof(rWrist) will simply produce the string "rWrist", and may be replaced by strings directly if that's preferred (e.g. ["Right wrist"] = rWrist rather than [nameof(rWrist)] = rWrist).)