Sum informations in file....

Alexandre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 08:05:31 PDT 2014


Ohhhh, real intresting the mode functional style!!!
Like linq!
hahah

Btw, it's work very well, thansk!!!
But, how I can insert an ',' comma to separe the decimal place ? 
( the last 2 digits )
I can't find a "insert" instruction in std.string or std.array

On Thursday, 10 July 2014 at 15:01:52 UTC, bearophile wrote:
> 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