-3

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.

ag4w
  • 1
  • 3
  • What do you mean when you say *how to identify the sign attributes in the textfile:*? What sign attribute? – CodingYoshi Feb 05 '18 at 22:31
  • Between enums in a text file, attributes in a text file, putting them in a `string[][][]`, and retrieving them with a random number, I suspect that most people are as lost as I am. – Scott Hannen Feb 05 '18 at 22:31
  • 1
    *"I want to create a string[][][] that stores all these strings"* No, I'm pretty certain you **don't** want to do that... – Rufus L Feb 05 '18 at 22:37
  • @ScottHannen I'm trying to sort the strings at a specific index, so I can cast my enums to return a random string from a specific selection. – ag4w Feb 05 '18 at 22:40
  • I'm genuinely sorry - I read your last comment over several times and I'm even more perplexed. – Scott Hannen Feb 05 '18 at 22:43
  • @ScottHannen I am in the same boat...totally lost – CodingYoshi Feb 05 '18 at 22:45
  • Alright, I'll try to explain: 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 casted as ints. @ScottHannen – ag4w Feb 05 '18 at 22:49
  • I think the first problem may be `string[][][]`. I'd recommend coming up with some sort of class structure that represents the data you want to store. When you define that, the class or classes may make the intended use of the data clearer. Also, if reading the data is just a means to being able to have it in memory and use it, then why this particular text format? It's probably better to go the other way. Design a class or set of classes that contains what you want and serialize it as JSON or XML. Then you don't have to write any code to parse it. – Scott Hannen Feb 05 '18 at 22:55
  • Well what's in the `[enum][enum]` line? Does it read `[1][2]` or does it read `[FoodEnum][DrinkEnum]`. – LarsTech Feb 05 '18 at 23:03
  • @LarsTech It's two separate enums. I'm trying to create a name generator for my game that randomises state names depending on input parameters (in this case it's [GovermentType] and [TechLevel]). – ag4w Feb 05 '18 at 23:10
  • So this is an enum parsing question from a string input? See [How should I convert a string to an enum in C#?](https://stackoverflow.com/q/16100/719186) – LarsTech Feb 05 '18 at 23:21

1 Answers1

0

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.

ag4w
  • 1
  • 3