ag4w
ag4w

Reputation: 1

Read all lines to string[] until next sign is found

I'm trying to read one textfile (one line per entry, separated by two "enums" used as attribute, see example).

[enum1_0][enum2_0]
line
line

[enum1_0][enum2_1]
line
line

[enum1_1][enum2_0]
line
line

[enum1_1][enum2_1]
line
line

I want to create a string[][][] that stores all these strings, so that I later can easily retrieve them using a method like this, but I'm having alot of trouble with the syntax and how to identify the sign attributes in the textfile:

public static string GetRandomString(Enum1 e1, Enum2 e2)
{
    return _strings[(int)e1][(int)e2][_random.Next(0, length)];
}

To clarify: I have a textfile that has one entry per line, where blocks of lines are separated by "[]"-marked 'signs' that correspond to every index of MyEnum1 and MyEnum2.

I'm trying to read all strings into a single string[], and then sort them into a string[][][] using System.File.IO.ReadAllLines, so that I can later use my function shown above to retrieve them using my enums.

Upvotes: -3

Views: 170

Answers (1)

ag4w
ag4w

Reputation: 1

So, after some proper sleep I reconsidered my structure, and this is my solution:

static List<GovermentData> _data = new List<GovermentData>();

static string[] _all = File.ReadAllLines(@"Assets/Resources/" + PREFIXES_FILEPATH + ".txt");
static string[] _nations = File.ReadAllLines(@"Assets/Resources/" + NAMES_FILEPATH + ".txt");

public static void Initialize()
{
    EconomicPolicy ep = EconomicPolicy.RadicalCollectivist;
    TechLevel tl = TechLevel.Ancient;

    //iterate all prefixes
    for (int i = 0; i < _all.Length; i++)
    {
        if (_all[i].Length <= 0)
            continue;

        if(_all[i].StartsWith("["))
        {
            string s = _all[i];

            //remove first [ and last ]
            s = s.Remove(0, 1);
            s = s.Remove(s.Length - 1, 1);

            //split on ][
            string[] r = s.Split(new string[] { "][" }, StringSplitOptions.None);

            ep = (EconomicPolicy)Enum.Parse(typeof(EconomicPolicy), r[0]);
            tl = (TechLevel)Enum.Parse(typeof(TechLevel), r[1]);
        }
        else
            _data.Add(new GovermentData(_all[i], ep, tl));
    }
}
public static string GetCivilisationName(EconomicPolicy ep, TechLevel t)
{
    GovermentData[] gd = _data.FindAll(d => d.economicPolicy == ep && d.techLevel == t).ToArray();

    if(gd.Length >= 1)
        return gd[0].name + " of " + _nations[_random.Next(0, _nations.Length)];
    else
        return gd[_random.Next(0, gd.Length)].name + " of " + _nations[_random.Next(0, _nations.Length)];
}

public struct GovermentData
{
    public readonly string name;
    public readonly EconomicPolicy economicPolicy;
    public readonly TechLevel techLevel;

    public GovermentData(string name, EconomicPolicy economicPolicy, TechLevel techLevel)
    {
        this.name = name;
        this.economicPolicy = economicPolicy;
        this.techLevel = techLevel;
    }
}

Thanks for trying to help me despite my own confusion.

Upvotes: 0

Related Questions