On Thursday, 2 February 2017 at 18:18:13 UTC, John Doe wrote:
Let's say you're trying to parse a file format like:
Name
http://example.com
123234
Foo Bar
http://dlang.org
888888
with blocks separated by varying amount of blank lines.
-----
import std.stdio;
void main(string[] args){
auto range = File("text.txt").byLine();
foreach( line; range ){
if (line != ""){
writeln(line);
// char[] url = range.???
// char[] num = range.???
}
}
}
-----
How can you read the next line while iterating over a file line
by line, so that the next iteration uses the line after next?
If this isn't possible byLine is a design flaw and D should
instead provide a regular readLine function.
btw: What is this? A forum for a programming language that
doesn't support code blocks?
If you understand the underlying range interface, the answer
becomes clear:
import std.stdio;
void main(string[] args)
{
auto range = File("text.txt").byLineCopy();
foreach (line; range)
{
if (line != "")
{
writeln(line);
range.popFront;
char[] url = range.front();
range.popFront;
char[] num = range.front();
}
}
}