| Thread overview | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 10, 2014 Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
I have one file with a lot of numeric data... and I need to sum all that data...
That is my actual code:
module main;
import std.stdio;
import std.file;
import std.conv : to;
int main(string[] args)
{
auto f = File("oi.txt");
auto r = f.byLine();
auto tot = 0;
foreach(line;r)
{
if(line[0] == '1')
tot += to!int(line[253..266]);
}
writeln(tot);
return 0;
}
I want to know if have a more better way to make this... maybe using lambda or tamplates....
| ||||
July 10, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Alexandre | 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
| |||
July 10, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
| |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Alexandre | I can't understand why foreach loop produce every line by line, while it's fuctional analog print lines on one string:
foreach(f; file.byLine())
{
writeln(f);
}
auto file = File("foo.txt","r");
file
.byLine()
.writeln;
file content:
-------------
first sring
second string
-------------
Output:
D:\code\JSONServer\source>app.exe
["first sring", "second string"]
expected:
first sring
second string
| |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman Attachments: | On Mon, 24 Nov 2014 19:04:34 +0000 Suliman via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I can't understand why foreach loop produce every line by line, while it's fuctional analog print lines on one string: the two samples are not the same, they doing completely different things. File.byLine returns *output* *range*. what `foreach` does is processing this range element by element, while `writeln(range)` outputs the whole range. the second code means: auto lines = file.byLine(); writeln(lines); for `writeln` output range is a kind of array, so it outputs it as an array. | |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 11/24/2014 11:04 AM, Suliman wrote:
> I can't understand why foreach loop produce every line by line, while
> it's fuctional analog print lines on one string:
>
> foreach(f; file.byLine())
> {
> writeln(f);
f is a char[] and writeln prints all strings as their contents i.e. "first string" is printed as
first string
> }
>
> auto file = File("foo.txt","r");
> file
> .byLine()
> .writeln;
In that case the first three lines make a range object. By default, writeln prints ranges as if they are arrays. For this example, the range is a range of strings, so it prints it as
[ "first string" ]
writefln gives us more power:
auto content = File("foo.txt","r").byLine();
writefln("%-(%s\n%)", content);
Per-element formatting is specified within %( and %). So, each element would be printed with "%s\n" format. Notes:
1) The dash in %-( means "do not print the double-quotes for strings"
2) Everything after %s in the per-element formatting is taken as element delimiter and writefln does not print the delimiters are not printed for the last element. One may need to use %| to specify the actual delimiters.
Here is the spec:
http://dlang.org/phobos/std_format.html#.formattedWrite
Here is my rewording:
http://ddili.org/ders/d.en/formatted_output.html
Ali
| |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | On 11/24/2014 11:30 AM, ketmar via Digitalmars-d-learn wrote: > File.byLine returns *output* *range*. Although the range is used for outputting, it is still an InputRange. :) Ali | |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli Attachments: | On Mon, 24 Nov 2014 11:41:43 -0800 Ali Çehreli via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On 11/24/2014 11:30 AM, ketmar via Digitalmars-d-learn wrote: > > > File.byLine returns *output* *range*. > > Although the range is used for outputting, it is still an InputRange. :) ah, yes, my bad. i'm always tend to mess with "input", "output", "client", "server" and such. knowing that i checked three times if i wrote the correct range direction and... failed it. | |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | thanks! But how I can skip first line? My varian: auto lines = "foo.txt".File .byLine .filter!(f=>f[0] != f[0]); | |||
November 24, 2014 Re: Sum informations in file.... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Monday, 24 November 2014 at 20:23:57 UTC, Suliman wrote:
> thanks! But how I can skip first line?
>
> My varian:
> auto lines = "foo.txt".File
> .byLine
> .filter!(f=>f[0] != f[0]);
With 'drop' from std.range:
auto lines = "foo.txt".File
.byLine
.drop(1)
.filter!(f=>f[0] != f[0]);
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply