views:

186

answers:

2
var t1 = from line in File.ReadAllLines(@"alkahf.txt")
                     let item = line.Split(new string[] {". "}, StringSplitOptions.RemoveEmptyEntries)
                     let verse = line.Split(new string[] { "\n. " }, StringSplitOptions.RemoveEmptyEntries)
                     select new
                     {
                         Index = item,
                         Text = verse
                     };

having problems with above code im unsure how to parse the lines properly.

the format of the file is like so, I would also like to ignore any empty lines StringSplitOptions.RemoveEmptyEntries doesn't work for some reason

1. This is text it might have numbers

2. I skipped a line
+1  A: 

In the LINQ part, you are inside a single line, so you might want to exclude the empty lines first:

from line in File.ReadAllLines(@"alkahf.txt")
where !string.IsNullOrEmpty(line)

You then do two splits - one on newline, which is odd (since that won't be there, since we know we are reading lines). I expect you mean something like:

let parts = line.Split('.')
where parts.Length == 2
select new {
    Index = parts[0],
    Text = parts[1]
};

?

Also, note that ReadAllLines is a buffered operation; if you want true streaming, you might want something like:

public static IEnumerable<string> ReadLines(string path) {
    using(var reader = File.OpenText(path)) {
        string line;
        while((line = reader.ReadLine()) != null) {
            yield return line;
        }
    }
}

which is not buffering (you don't load the entire file at once). Just change the first line to:

from line in ReadLines(@"alkahf.txt")
Marc Gravell
thank you for your quick response, but the index will be off when 2 or more digits are added to the sequence when your checking for length. For example 178. Im the two added digits
Ayo
@Ayo - then perhaps make the sample input and desired output clear in the question?
Marc Gravell
toshay, but I am trying to create a dynamic solution, is there a way to split the string based off datatypes? I was looking at regex.Split but its too much of a complicated solution for my purposes.
Ayo
@Ayo - again, it would depend on what *exactly* you mean...
Marc Gravell
@Marc - Again sorry for the ambiguity of my question, what is trying to happen here is I have a line where I want a split to occur between the first instance of an integer(hence the need to determine datatypes) followed by a ". "(period + whitespace) the rest of the line is to be buffered for the last portion of the object.As well the last portion could have periods and numbers so being specific is key. Hopefully this answered any confusion with my question, and your help is greatly appreciated Marc.
Ayo
A: 

thanks to Marc's I fixed my issue, sorry for the late response I'm working on this as a personal project.

the code is like so

 var t1 = from line in StreamReaderExtension.ReadLinesFromFile(@"alkahf.txt")
                     let parts = line.Split(new string[]{". "},StringSplitOptions.RemoveEmptyEntries)
                     where !string.IsNullOrEmpty(line)                     
                     && int.Parse(parts[0].ToString()).ToString() != ""
                     select new
                     {
                         Index = parts[0],
                         Text = parts[1]
                     };

the int parse addition makes sure that the input is returning an integer, if your using this code it's a good idea to set a flag in case it picks ups a non-integer or it will go unnoticed.

Ayo