Sum informations in file....
bearophile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jul 10 08:01:51 PDT 2014
Alexandre:
> I want to know if have a more better way to make this... maybe
> using lambda or tamplates....
Your code is not bad. This is a bit better (untested):
void main() {
import std.stdio;
import std.conv: to;
auto lines = "oi.txt".File.byLine;
int tot = 0;
foreach (const line; lines) {
if (line[0] == '1')
tot += line[253 .. 266].to!int;
}
tot.writeln;
}
If you want to write in a mode functional style (untested)
(probably it requires the 2.066beta):
void main() {
import std.stdio, std.algorithm, std.range, std.conv;
"oi.txt"
.File
.byLine
.filter!(line => line[0] == '1')
.map!(line => line[253 .. 266].to!int)
.sum
.writeln;
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list