On Monday, 14 May 2012 at 09:00:14 UTC, simendsjo wrote:
On Mon, 14 May 2012 10:41:54 +0200, Christian Köstlin <christian.koest...@gmail.com> wrote:

Hi,

i wanted to output an ascii-file line by line, but reversed.
the idea was to open the file, use byLine to read it line-by-line, make this range an array, and retro this array.

But when i convert byLine to an array, the result is already trash.

Please see this snippet.

import std.stdio;
import std.array;

int main(string[] args) {
  if (args.length != 2) {
    throw new Exception("Usage: " ~ args[0] ~ " file1");
  }

  auto f = File(args[1], "r");
  auto i = array(f.byLine());
  foreach (l; i) {
    writeln(l);
  }
  return 0;
}

Any idea why this happens?

regards

christian köstlin

I believe byLine reuses the internal buffer. Try duping the lines:



  auto i = f.byLine().map!"a.idup"().array();


Can someone please explain to me the last line?

I'm trying to learn D, by playing with code and reading this forum. I'm a slow learner. :)

Anyways, I looked at std.stdio code and noticed that byLine resturns a struct ByLine, but where does the .map come from? Thanks!

Reply via email to