C
C#11mo ago
Ewan

Someone help me with this question please

The test data is whats in the file and has tons of lines of those but i have no idea how to answer this with code, help please!
No description
8 Replies
Ewan
EwanOP11mo ago
i suck at file coding
Ewan
EwanOP11mo ago
No description
Ewan
EwanOP11mo ago
ive got to about here so far but also have no idea how to read each line and move onto the next and idk how to make each line an array so the split array can split the lines into an array
Angius
Angius11mo ago
Just so you know, File.ReadAllLinesAsync() will just read the file lines into a string[] No need for this whole song and dance with streamreader You will, then, need to parse each line you have The format seems to be owner:[dog:color,dog:color] so it should be fairly easy to parse Split on : first to get an array of [ owner, dogs ] then trim [ and ] from dogs and split on , Then split each dog on : to get [dog, color] Ideally, you're looking to parse the file into something like List<Person> where
class Person
{
public string Name { get; set; }
public Dictionary<string, string> Dogs { get; set; }
}
class Person
{
public string Name { get; set; }
public Dictionary<string, string> Dogs { get; set; }
}
Or
class Person
{
public string Name { get; set; }
public List<Dog> Dogs { get; set; }
}
class Dog
{
public string Name { get; set; }
public string CollarColor { get; set; }
}
class Person
{
public string Name { get; set; }
public List<Dog> Dogs { get; set; }
}
class Dog
{
public string Name { get; set; }
public string CollarColor { get; set; }
}
Oh, and to loop over all the lines... use a loop foreach will work just fine To split a line you use... .Split() With a delimiter, of course. For example:
MODiX
MODiX11mo ago
Angius
REPL Result: Success
var line = "Billy Bob:Rex";
line.Split(':')
var line = "Billy Bob:Rex";
line.Split(':')
Result: string[]
[
"Billy Bob",
"Rex"
]
[
"Billy Bob",
"Rex"
]
Compile: 348.807ms | Execution: 23.012ms | React with ❌ to remove this embed.
Angius
Angius11mo ago
Huh, now that I look at the format, just splitting on : won't be sufficient, since it will split all the dogs as well... Writing a quick parser would probably be the best, something like...
MODiX
MODiX11mo ago
Angius
REPL Result: Success
var line = "Billy Bob:[Rex:Red,Fluffy:Green]";

var person = "";
var dogs = "";
var parsingDogs = false;
foreach (var ch in line)
{
if (ch == ':') parsingDogs = true;
if (parsingDogs) dogs += ch;
else person += ch;
}

new { person, dogs }
var line = "Billy Bob:[Rex:Red,Fluffy:Green]";

var person = "";
var dogs = "";
var parsingDogs = false;
foreach (var ch in line)
{
if (ch == ':') parsingDogs = true;
if (parsingDogs) dogs += ch;
else person += ch;
}

new { person, dogs }
Result: <>f__AnonymousType0#1<string, string>
{
"person": "Billy Bob",
"dogs": ":[Rex:Red,Fluffy:Green]"
}
{
"person": "Billy Bob",
"dogs": ":[Rex:Red,Fluffy:Green]"
}
Compile: 303.551ms | Execution: 80.188ms | React with ❌ to remove this embed.
Ewan
EwanOP11mo ago
oooo ok i think i get what u mean ill try this out! thank you!! catlaugh
Want results from more Discord servers?
Add your server