views:

72

answers:

0

My programmer's block is especially bad today, maybe you can help get me moving again?!


Supplement

I'm trying to parse out some CSV's from a legacy program we are trying to support. The part I'm trying to abstract is stored by the old program in a string "Channel.Device.Element1.Element2.Tag:Description"

I am now storing this data along with some extended data used in our program.

I have objects that create a hierarchical data structure (basically a tree) to store this data:

note: I've omitted the unrelated code

public class Container
{
    List<DataElement> Elements;

    ...
    ...

            public Container()
            {
                    Elements = New List<DataElement> Elements;

                    ...
                    ...
            }
}

public class DataElement
{
    string Name;
    List<DataElement> SubElements;
    List<DataTag> Tags;

    public DataElement(string Name)
    {
        this.Name = Name;
        SubElements = new List<DataElement>();
        Tags = new List<DataTag>();
    }
}

public class DataTag
{
    string Name;
    string Description;

    public DataTag(string Name, string Description)
    {
        this.Name = Name;
        this.Description = Description;
    }

    ...
    ...
}

I know the best option is to use a recursive function to transverse my structure, but for parsing large amounts of data I think it will be faster to manage a list of the groups in a Dictionary<string, DataElement> where the Key is the entire Group string.

These are example strings that I've parsed from the legacy format.

"blah.1.device.ctrl.watchdog:watchdog value"
"blah.1.device.unit.speed:unit speed"
"blah.1.device.unit.speed_sp:unit speed setpoint"

Question

What would be the best way populate my Container class using a . delimited string for the elements or nodes resulting in the following structure?

Container:Name=>Data
    | - DataElement:Name=>blah
    |   | - DataElement:Name=1
    |   |   | - DataElement:Name=>device
    |   |   |   | - DataElement:Name=>ctrl
    |   |   |   |   | - DataTag:Name=>speed, Description=>unit speed, ...
    |   |   |   |   | - DataTag:Name=>speed_sp, Description=>unit speed setpoint, ...
    |   |   |   |   |
    |   |   |   | - DataElement:Name=unit
    |   |   |   |   | - DataTag:Name=>watchdog, Description=>watchdog value, ...
    |   |   |   |   |