www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Sum informations in file....

reply "Alexandre" <alebencz gmail.com> writes:
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....
Jul 10 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 10 2014
parent reply "Alexandre" <alebencz gmail.com> writes:
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
Jul 10 2014
parent reply "Suliman" <evermind live.ru> writes:
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
Nov 24 2014
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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,=20
 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 =3D file.byLine(); writeln(lines); for `writeln` output range is a kind of array, so it outputs it as an array.
Nov 24 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 24 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 24 Nov 2014 11:41:43 -0800
Ali =C3=87ehreli via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 On 11/24/2014 11:30 AM, ketmar via Digitalmars-d-learn wrote:
=20
  > File.byLine returns *output* *range*.
=20
 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.
Nov 24 2014
parent reply "Suliman" <evermind live.ru> writes:
thanks! But how I can skip first line?

My varian:
auto lines = "foo.txt".File
		.byLine
		.filter!(f=>f[0] != f[0]);
Nov 24 2014
parent reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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]);
Nov 24 2014
parent "Suliman" <evermind live.ru> writes:
Thanks! But is there any way to do it with std.algorithm?
Can be skipOver used for it?
Nov 24 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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: Here is my rewording: http://ddili.org/ders/d.en/formatted_output.html Ali
Nov 24 2014