digitalmars.D.learn - Read Complete File to Array of Lines
- Paul (6/6) May 11 2012 I would like to read a complete file in one statement and then
- sclytrack (29/35) May 11 2012 ---SOURCE
- H. S. Teoh (19/26) May 11 2012 import std.array;
- Graham Fawcett (21/38) May 11 2012 Does that work? I think you mean:
- Jesse Phillips (7/13) May 11 2012 Something like:
- Paul (5/20) May 11 2012 Thanks Jesse.
- Era Scarecrow (7/15) May 11 2012 Why not?
- Paul (39/55) May 11 2012 This is my program:
- Era Scarecrow (9/11) May 11 2012 What are you reading? If it's regular text (0-127) then you
- Suliman (18/18) May 06 2014 I am trying to write simple parser, that split text to
- Graham Fawcett (12/18) May 11 2012 If you use the "byLine" approach...
- Steven Schveighoffer (4/10) May 11 2012 Would something like this work?
- Graham Fawcett (11/23) May 11 2012 It sure would. I suspect that Jesse's approach...
- Era Scarecrow (6/11) May 11 2012 Doesn't sound hard.. I could likely write a quick line splitting
- Era Scarecrow (3/11) May 11 2012 Actually re-reading that and glancing at splitLines, I see it's
- Graham Fawcett (5/18) May 11 2012 Hold on! It already exists:
- Paul (4/31) May 11 2012 GULP! This language is so powerful! Do I dare ask a followup
I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? Thanks
May 11 2012
On 05/11/2012 05:00 PM, Paul wrote:I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? Thanks---SOURCE import std.stdio; import std.file; int main() { writeln("start"); foreach( line; File("src/main.d").byLine()) { writeln(line); } writeln("stop"); return 0; } ---OUTPUT start import std.stdio; import std.file; int main() { writeln("start"); foreach( line; File("src/main.d").byLine()) { writeln(line); } writeln("stop"); return 0; } stop
May 11 2012
On Fri, May 11, 2012 at 05:00:16PM +0200, Paul wrote:I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines?import std.array; import std.stdio; string[] getLines(File f) { auto a = appender!string; foreach (line; f.byLine()) { a.put(line); } return a.data; } void main(string[] args) { if (args > 1) { auto f = File(args[0]); auto lines = getLines(f); } } T -- "How are you doing?" "Doing what?"
May 11 2012
On Friday, 11 May 2012 at 15:18:11 UTC, H. S. Teoh wrote:On Fri, May 11, 2012 at 05:00:16PM +0200, Paul wrote:Does that work? I think you mean: string[] getLines(File f) { auto a = appender!(string[]); foreach (line; f.byLine()) { a.put(line.idup); } return a.data; } You could also write: string[] getLines(File f) { return array(map!"a.idup"(f.byLine)); } Or { return f.byLine.map!"a.idup".array; } This may be slower than the appender version, which is optimized for performance; I'm not sure. GrahamI would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines?import std.array; import std.stdio; string[] getLines(File f) { auto a = appender!string; foreach (line; f.byLine()) { a.put(line); } return a.data; }
May 11 2012
On Friday, 11 May 2012 at 15:00:18 UTC, Paul wrote:I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? ThanksSomething like: import std.file; import std.string; void main() { foreach(line; readText("file.in").splitLines()) ... }
May 11 2012
On Friday, 11 May 2012 at 18:02:54 UTC, Jesse Phillips wrote:On Friday, 11 May 2012 at 15:00:18 UTC, Paul wrote:Thanks Jesse. I'm finding that I can't just substitute args[1] for a text string. Is there a clever way to use args[] in place of your "file.in"?I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? ThanksSomething like: import std.file; import std.string; void main() { foreach(line; readText("file.in").splitLines()) ... }
May 11 2012
On Friday, 11 May 2012 at 20:40:23 UTC, Paul wrote:On Friday, 11 May 2012 at 18:02:54 UTC, Jesse Phillips wrote:Why not? void main(string[] args) { if(args.length > 1) { foreach(line; readText(args[1]).splitLines()) ... } }void main() { foreach(line; readText("file.in").splitLines()) ... }Thanks Jesse. I'm finding that I can't just substitute args[1] for a text string. Is there a clever way to use args[] in place of your "file.in"?
May 11 2012
On Friday, 11 May 2012 at 20:43:47 UTC, Era Scarecrow wrote:On Friday, 11 May 2012 at 20:40:23 UTC, Paul wrote:This is my program: import std.stdio,std.string,std.file; // Main function void main(string[] args){ if (args.length > 1){ foreach(line; readText(args[1]).splitLines()) { writefln("line : %s", line); //if (n > 10) break; } } } I get this: std.utf.UTFException std\utf.d(644): Invalid UTF-8 sequence (at index 1) ---------------- 41E424 41E29B 4020F3 4020C1 402042 4077A4 4077E3 4073F3 438781 ---------------- These are the first few lines of my text file: NAME = XPAW01_STA TYPE = COMPND DESCRP = PERIOD = 1 PHASE = 0 ON = 0 INITON = 2 CINHIB = 0 GR1DV1 = GR1DV2 = GR1DV3 = GR1DV4 =On Friday, 11 May 2012 at 18:02:54 UTC, Jesse Phillips wrote:Why not? void main(string[] args) { if(args.length > 1) { foreach(line; readText(args[1]).splitLines()) ... } }void main() { foreach(line; readText("file.in").splitLines()) ... }Thanks Jesse. I'm finding that I can't just substitute args[1] for a text string. Is there a clever way to use args[] in place of your "file.in"?
May 11 2012
On Friday, 11 May 2012 at 21:13:41 UTC, Paul wrote:std.utf.UTFException std\utf.d(644): Invalid UTF-8 sequence (at index 1)What are you reading? If it's regular text (0-127) then you shouldn't have an issue. However 128-255 (or, -1 to -127) are treated differently. D by default is UTF-8 or unicode encoded, meaning it wants to see valid encodings that follow that structure. Unfortunately I don't have good references of how to simply convert old Ascii to a utf-8 (within Phobos). I have my own workaround...
May 11 2012
I am trying to write simple parser, that split text to key value name = david lastname = wood here is my code: foreach (line; readText(confname).splitLines()) { writeln(line); foreach (str; split(line, "=")) { writeln(str); } } now it's look like my code are put at str something wrong. I tried to do writeln(str[0]) but it's put there only first letters from first row. Also could anybody help me to find example how create structure and than fill it's with value field.
May 06 2014
On Friday, 11 May 2012 at 15:00:18 UTC, Paul wrote:I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? ThanksIf you use the "byLine" approach... foreach(line; File("myfile").byLine)) { ... } or File("myfile").byLine.doSomethingWithLines() ...keep in mind that "byLine", as it traverses your file, reuses a single buffer to store the current line in. If you're storing those lines somewhere for later use, use "line.dup" or "line.idup" to make a copy of that buffer. (Or use a different approach, like readText/splitLines.) That was one of the first "gotcha" moments I had with D. :) Graham
May 11 2012
On Fri, 11 May 2012 11:00:16 -0400, Paul <phshaffer gmail.com> wrote:I would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? ThanksWould something like this work? auto arr = array(map!"a.idup"(File("file.txt").byLine())); -Steve
May 11 2012
On Friday, 11 May 2012 at 18:57:52 UTC, Steven Schveighoffer wrote:On Fri, 11 May 2012 11:00:16 -0400, Paul <phshaffer gmail.com> wrote:It sure would. I suspect that Jesse's approach... readText("file.in").splitLines() ...would be the most efficient way if you need an actual array: slurp the whole file at once, then create an array of memory-sharing slices. I look forward to the great std.stdio/std.file unification of 201x, when I won't have to look in two modules for file-reading functions. :) GrahamI would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? ThanksWould something like this work? auto arr = array(map!"a.idup"(File("file.txt").byLine()));
May 11 2012
On Friday, 11 May 2012 at 19:24:49 UTC, Graham Fawcett wrote:It sure would. I suspect that Jesse's approach... readText("file.in").splitLines() ...would be the most efficient way if you need an actual array: slurp the whole file at once, then create an array of memory-sharing slices.Doesn't sound hard.. I could likely write a quick line splitting ranges fairly quickly, assuming \r and \n are the newlines, if there's new ones I'm unaware of then it may not work quite as well as you want :P Let's see....
May 11 2012
On Friday, 11 May 2012 at 20:06:45 UTC, Era Scarecrow wrote:On Friday, 11 May 2012 at 19:24:49 UTC, Graham Fawcett wrote:Actually re-reading that and glancing at splitLines, I see it's already done... Mmmm... ignore my post(s) I guess.It sure would. I suspect that Jesse's approach... readText("file.in").splitLines() ...would be the most efficient way if you need an actual array: slurp the whole file at once, then create an array of memory-sharing slices.
May 11 2012
On Friday, 11 May 2012 at 20:06:45 UTC, Era Scarecrow wrote:On Friday, 11 May 2012 at 19:24:49 UTC, Graham Fawcett wrote:Hold on! It already exists: std.file.readText std.string.splitLines GrahamIt sure would. I suspect that Jesse's approach... readText("file.in").splitLines() ...would be the most efficient way if you need an actual array: slurp the whole file at once, then create an array of memory-sharing slices.Doesn't sound hard.. I could likely write a quick line splitting ranges fairly quickly, assuming \r and \n are the newlines, if there's new ones I'm unaware of then it may not work quite as well as you want :P Let's see....
May 11 2012
On Friday, 11 May 2012 at 19:24:49 UTC, Graham Fawcett wrote:On Friday, 11 May 2012 at 18:57:52 UTC, Steven Schveighoffer wrote:GULP! This language is so powerful! Do I dare ask a followup question? I will try the readText.splitLines(). Thanks to all!On Fri, 11 May 2012 11:00:16 -0400, Paul <phshaffer gmail.com> wrote:It sure would. I suspect that Jesse's approach... readText("file.in").splitLines() ...would be the most efficient way if you need an actual array: slurp the whole file at once, then create an array of memory-sharing slices. I look forward to the great std.stdio/std.file unification of 201x, when I won't have to look in two modules for file-reading functions. :) GrahamI would like to read a complete file in one statement and then process it line by line. foreach (line; MyFile) etc. Is it possible to read a file into and array of lines? ThanksWould something like this work? auto arr = array(map!"a.idup"(File("file.txt").byLine()));
May 11 2012