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:
>> 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....

Hold on! It already exists:

std.file.readText
std.string.splitLines

Graham

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:
>> 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?
>>> Thanks
>>
>> Would something like this work?
>>
>> auto arr = array(map!"a.idup"(File("file.txt").byLine()));
>>
>
> 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. :)
>
> Graham
GULP!  This language is so powerful!  Do I dare ask a followup question?
I will try the readText.splitLines().  Thanks to all!


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:
>> 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
>
> Something like:
>
> import std.file;
> import std.string;
>
> 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:40:23 UTC, Paul wrote:
> On Friday, 11 May 2012 at 18:02:54 UTC, Jesse Phillips wrote:
>> 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"?

Why not?

void main(string[] args) {
    if(args.length > 1) {
        foreach(line; readText(args[1]).splitLines()) ...
    }
}
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:
>> On Friday, 11 May 2012 at 18:02:54 UTC, Jesse Phillips wrote:
>>> 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"?
>
> Why not?
>
> void main(string[] args) {
>     if(args.length > 1) {
>         foreach(line; readText(args[1]).splitLines()) ...
>     }
> }

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 =

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 06, 2014
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.
1 2
Next ›   Last »