Re: reading file byLine

2015-11-07 Thread Namal via Digitalmars-d-learn
On Saturday, 7 November 2015 at 17:13:33 UTC, Namal wrote: On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote: On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote: .map!(s => chomp(s, "\"") .map!(s => chompPrefix(s, "\"") should be .map!(s => chomp(s, "\"")) .map!(s =>

Re: reading file byLine

2015-11-07 Thread Namal via Digitalmars-d-learn
On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote: On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote: .map!(s => chomp(s, "\"") .map!(s => chompPrefix(s, "\"") should be .map!(s => chomp(s, "\"")) .map!(s => chompPrefix(s, "\"")) Hello again, Now I have a file that

Re: reading file byLine

2015-09-18 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Friday, 18 September 2015 at 10:48:25 UTC, Namal wrote: On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van Leeuwen wrote: On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote: Hello guys, is there a nice functional way to read the file which is like 1,2,3,4,5,6 2,3,4,5,6,7

Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
Hello guys, is there a nice functional way to read the file which is like 1,2,3,4,5,6 2,3,4,5,6,7 8,9,0,9,2,3 line by line, split numbers and remove each ',' convert it to int and save in a matrix int[][] arr?

Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van Leeuwen wrote: On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote: Hello guys, is there a nice functional way to read the file which is like 1,2,3,4,5,6 2,3,4,5,6,7 8,9,0,9,2,3 line by line, split numbers and remove each ','

Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
On Friday, 18 September 2015 at 11:37:15 UTC, Edwin van Leeuwen wrote: On Friday, 18 September 2015 at 11:11:51 UTC, Namal wrote: compiles but crashes For me it works fine. You probably have extra spaces or something in your file. It would help if you posted the error message you get when

Re: reading file byLine

2015-09-18 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote: Hello guys, is there a nice functional way to read the file which is like 1,2,3,4,5,6 2,3,4,5,6,7 8,9,0,9,2,3 line by line, split numbers and remove each ',' convert it to int and save in a matrix int[][] arr? Not tested, but I

Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
On Friday, 18 September 2015 at 11:06:46 UTC, Edwin van Leeuwen wrote: On Friday, 18 September 2015 at 10:48:25 UTC, Namal wrote: On Friday, 18 September 2015 at 10:34:41 UTC, Edwin van Leeuwen wrote: On Friday, 18 September 2015 at 10:26:46 UTC, Namal wrote: Hello guys, is there a nice

Re: reading file byLine

2015-09-18 Thread Namal via Digitalmars-d-learn
So do I understand it right: does => in map! indicates a lambda function?

Re: reading file byLine

2015-09-18 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Friday, 18 September 2015 at 12:28:29 UTC, Namal wrote: So do I understand it right: does => in map! indicates a lambda function? Yes exactly. There are a number of ways you can define a lambda function in D. For example if the function is multiline I often use: (l) { ...; // do

Re: reading file byLine

2015-09-18 Thread Ali Çehreli via Digitalmars-d-learn
On 09/18/2015 05:58 AM, Edwin van Leeuwen wrote: On Friday, 18 September 2015 at 12:28:29 UTC, Namal wrote: So do I understand it right: does => in map! indicates a lambda function? Yes exactly. There are a number of ways you can define a lambda function in D. For example if the function is

Re: reading file byLine

2015-09-14 Thread deed via Digitalmars-d-learn
On Monday, 14 September 2015 at 18:36:54 UTC, Meta wrote: As an aside, you should use `sort()` instead of the parentheses-less `sort`. The reason for this is that doing `arr.sort` invokes the old builtin array sorting which is terribly slow, whereas `import std.algorithm; arr.sort()` uses the

Re: reading file byLine

2015-09-14 Thread Meta via Digitalmars-d-learn
On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote: Right, it's like int x = 3; // x + 5; // Just an expression evaluated to 8, // but what do you want to do with it? // It won't affect your program and the // compiler will give you an

Re: reading file byLine

2015-09-13 Thread deed via Digitalmars-d-learn
On Sunday, 13 September 2015 at 03:20:31 UTC, deed wrote: string s = "Some text"; s.retro.find("e"); // `Some te` (Surprising to me. Error? 2.067.1) Sorry, the above is wrong, .retro.find does indeed return what's expected. string s = "Some text"; s.retro.find("e").writeln; //

Re: reading file byLine

2015-09-13 Thread deed via Digitalmars-d-learn
On Sunday, 13 September 2015 at 03:20:31 UTC, deed wrote: ... and since `string` is an alias for `const(char)[]`, it's not ... string is an alias for immutable(char)[], not const(char)[]. http://dlang.org/arrays.html#strings Sorry about the noise.

Re: reading file byLine

2015-09-12 Thread deed via Digitalmars-d-learn
On Saturday, 12 September 2015 at 12:51:04 UTC, Namal wrote: Anyway, there is no .reverse for strings I guess, what is the way to completely reverse a string in D? What do you want to do? Do you want to keep your data in original order, but get a reversed view of it for something, or do you

Re: reading file byLine

2015-09-12 Thread Namal via Digitalmars-d-learn
On Monday, 7 September 2015 at 10:28:20 UTC, deed wrote: On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote: writeln(x);// or you can pass it to a function. I meant `writeln(x + 5)` If I have just red your post before I started using reverse on dynamic arrays... Anyway, there is

Re: reading file byLine

2015-09-07 Thread deed via Digitalmars-d-learn
On Sunday, 6 September 2015 at 22:04:55 UTC, Namal wrote: oh, sorry. But I found out what I have been doing wrong besides that. arr.sort.uniq; uniq(arr) or arr.sort.uniq; compiles but doesn't store it in the arr array, I need to store it in a new one. Right, it's like int x = 3; // x + 5;

Re: reading file byLine

2015-09-07 Thread deed via Digitalmars-d-learn
On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote: writeln(x);// or you can pass it to a function. I meant `writeln(x + 5)`

Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
On Sunday, 6 September 2015 at 20:39:27 UTC, deed wrote: On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote: Yeah, I just checked, it is 2.066, how can I install the new version on ubuntu with sudo apt-get? sudo apt-get install dmd will give you dmd v2.067.1. Don't know when it will

Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
That should be it though... Could you try this minimal complete test? import std.stdio; import std.algorithm; void main(string[] args) { int[] arr = [1, 2, 4, 2, 3, 4, 1]; arr.sort.uniq.writeln; } // [1, 2, 3, 4] yes, it works likte that. unique(arr) I get Error: undefined

Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
Well, if you don't type function names right, it will be hard to help you. oh, sorry. But I found out what I have been doing wrong besides that. arr.sort.uniq; uniq(arr) or arr.sort.uniq; compiles but doesn't store it in the arr array, I need to store it in a new one.

Re: reading file byLine

2015-09-06 Thread cym13 via Digitalmars-d-learn
On Sunday, 6 September 2015 at 21:01:09 UTC, Namal wrote: On Sunday, 6 September 2015 at 20:39:27 UTC, deed wrote: On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote: Yeah, I just checked, it is 2.066, how can I install the new version on ubuntu with sudo apt-get? sudo apt-get

Re: reading file byLine

2015-09-06 Thread cym13 via Digitalmars-d-learn
On Sunday, 6 September 2015 at 21:18:28 UTC, Namal wrote: That should be it though... Could you try this minimal complete test? import std.stdio; import std.algorithm; void main(string[] args) { int[] arr = [1, 2, 4, 2, 3, 4, 1]; arr.sort.uniq.writeln; } // [1, 2, 3, 4] yes, it

Re: reading file byLine

2015-09-06 Thread deed via Digitalmars-d-learn
On Sunday, 6 September 2015 at 17:57:49 UTC, Namal wrote: Yeah, I just checked, it is 2.066, how can I install the new version on ubuntu with sudo apt-get? sudo apt-get install dmd will give you dmd v2.067.1. Don't know when it will be upgraded to 2.068 though.

Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
Are you on 2.066 or older? Back then std.algorithm hasn't been split into submodules yet. Just import std.algorithm then instead of std.algorithm.comparison, std.algorithm.iteration, etc. Yeah, I just checked, it is 2.066, how can I install the new version on ubuntu with sudo apt-get? I

Re: reading file byLine

2015-09-06 Thread anonymous via Digitalmars-d-learn
On Sunday, 6 September 2015 at 15:41:34 UTC, Namal wrote: is there any function that removes double elements in a sorted array? std.algorithm.iteration.uniq http://dlang.org/phobos/std_algorithm_iteration.html#uniq

Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
Note that there's a specialized `std.algorithm.iteration.sum`. is there any function that removes double elements in a sorted array?

Re: reading file byLine

2015-09-06 Thread Namal via Digitalmars-d-learn
On Sunday, 6 September 2015 at 15:52:38 UTC, anonymous wrote: On Sunday, 6 September 2015 at 15:41:34 UTC, Namal wrote: is there any function that removes double elements in a sorted array? std.algorithm.iteration.uniq http://dlang.org/phobos/std_algorithm_iteration.html#uniq Hmm, I get

Re: reading file byLine

2015-09-06 Thread anonymous via Digitalmars-d-learn
On Sunday, 6 September 2015 at 16:17:29 UTC, Namal wrote: Error: module comparison is in file 'std/algorithm/comparison.d' which cannot be read import path[0] = /usr/include/dmd/phobos import path[1] = /usr/include/dmd/druntime/import when I try to load the headers like in the example Are

Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn
On Saturday, 5 September 2015 at 18:57:52 UTC, deed wrote: On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote: Yeah, I have have been trying this example from wiki books https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler It is not even compiling. What exactly is not

Re: reading file byLine

2015-09-05 Thread deed via Digitalmars-d-learn
On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote: Yeah, I have have been trying this example from wiki books https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler It is not even compiling. What exactly is not compiling?

Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn
On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote: On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote: .map!(s => chomp(s, "\"") .map!(s => chompPrefix(s, "\"") should be .map!(s => chomp(s, "\"")) .map!(s => chompPrefix(s, "\"")) Yeah, I have have been trying this

Re: reading file byLine

2015-09-05 Thread deed via Digitalmars-d-learn
On Saturday, 5 September 2015 at 12:41:37 UTC, Namal wrote: Thx guys. Now I try out the split function. I read the file as a single string? auto arr = split(cast(string)read(filename),","); where the file has "A", "B", "C" and I get the output ["\"A\"", " \"B\"", " \"C\"\n"] I can

Re: reading file byLine

2015-09-05 Thread Namal via Digitalmars-d-learn
On Friday, 4 September 2015 at 12:09:19 UTC, Edwin van Leeuwen wrote: On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen wrote: On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote: import std.algorithm, std.range, std.array, std.string, std.stdio, std.conv; int[] arr1 = [1,

Re: reading file byLine

2015-09-05 Thread deed via Digitalmars-d-learn
On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote: .map!(s => chomp(s, "\"") .map!(s => chompPrefix(s, "\"") should be .map!(s => chomp(s, "\"")) .map!(s => chompPrefix(s, "\""))

Re: reading file byLine

2015-09-04 Thread deed via Digitalmars-d-learn
On Friday, 4 September 2015 at 07:27:54 UTC, Namal wrote: On Friday, 4 September 2015 at 01:55:13 UTC, deed wrote: On Friday, 4 September 2015 at 01:31:28 UTC, Namal wrote: How can I get just the maximum element? Do I need to give a range for it? Use max?

Re: reading file byLine

2015-09-04 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen wrote: On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote: import std.algorithm, std.range, std.array, std.string, std.stdio, std.conv; int[] arr1 = [1, 2, 30]; //arr1.max.writeln; // Doesn't work, as you say

Re: reading file byLine

2015-09-04 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote: import std.algorithm, std.range, std.array, std.string, std.stdio, std.conv; int[] arr1 = [1, 2, 30]; //arr1.max.writeln; // Doesn't work, as you say arr1.reduce!max.writeln;// This does. Prints 30. Again using reduce is

Re: reading file byLine

2015-09-04 Thread Namal via Digitalmars-d-learn
On Friday, 4 September 2015 at 01:55:13 UTC, deed wrote: On Friday, 4 September 2015 at 01:31:28 UTC, Namal wrote: How can I get just the maximum element? Do I need to give a range for it? Use max? http://dlang.org/phobos/std_algorithm_comparison.html#max Sorry, I don't understand the

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 22:19:11 UTC, wobbles wrote: On Wednesday, 2 September 2015 at 21:53:20 UTC, Namal wrote: Thx guys, this helped alot. The next thing I want to do is read the file line by line and split the stream into words. I found this example of code that seems to do sort

Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 3 September 2015 at 22:48:01 UTC, Jordan Wilson wrote: On Thursday, 3 September 2015 at 22:21:57 UTC, Namal wrote: ep18.d(10): Error: no property 'split' for type 'char[]' /usr/include/dmd/phobos/std/algorithm.d(427): instantiated from here: MapResult!(__lambda1,

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
Sorry, I didn't notice the "convert all the elements in it to integer" part. I think I saw reference to the to! before...that is one way to convert. auto words = file.byLine() // you've all lines in range .map!(a => a.split) .map!(a =>

Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
Actually, need an extra map I think: auto word = file.byLine() .map!(a => a.split) .map!(a => map!(a => to!int(a))(a)) .array();

Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Sep 04, 2015 at 12:18:14AM +, Namal via Digitalmars-d-learn wrote: [...] > That being said, when do I have to import std.array and std.string? > Every time I use std.array? I can obviously use arrays and strings > without those libs. Arrays and strings are built into the language; but

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
On Thursday, 3 September 2015 at 23:54:44 UTC, H. S. Teoh wrote: On Thu, Sep 03, 2015 at 11:38:54PM +, Namal via Digitalmars-d-learn wrote: On Thursday, 3 September 2015 at 23:31:27 UTC, Jordan Wilson wrote: >On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote: >>On Thursday, 3

Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 3 September 2015 at 22:21:57 UTC, Namal wrote: ep18.d(10): Error: no property 'split' for type 'char[]' /usr/include/dmd/phobos/std/algorithm.d(427): instantiated from here: MapResult!(__lambda1, ByLine!(char, char)) ep18.d(10):instantiated from here:

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
On Thursday, 3 September 2015 at 23:31:27 UTC, Jordan Wilson wrote: On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote: On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote: And also: import std.algorithm Sorry, I should have taken the time to answer properly and fully.

Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 03, 2015 at 11:38:54PM +, Namal via Digitalmars-d-learn wrote: > On Thursday, 3 September 2015 at 23:31:27 UTC, Jordan Wilson wrote: > >On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote: > >>On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote: > >>>And also:

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
ep18.d(10): Error: no property 'split' for type 'char[]' /usr/include/dmd/phobos/std/algorithm.d(427): instantiated from here: MapResult!(__lambda1, ByLine!(char, char)) ep18.d(10):instantiated from here: map!(ByLine!(char, char)) and then a long list to the end of my code

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote: And also: import std.algorithm Sorry, I should have taken the time to answer properly and fully. import std.file, std.stdio, std.string, std.conv, std.algorithm; void main(){ auto file = File("text.txt");

Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
And also: import std.algorithm Sorry, I should have taken the time to answer properly and fully.

Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 3 September 2015 at 23:28:37 UTC, Namal wrote: On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote: And also: import std.algorithm Sorry, I should have taken the time to answer properly and fully. import std.file, std.stdio, std.string, std.conv, std.algorithm;

Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 03, 2015 at 11:22:09PM +, Namal via Digitalmars-d-learn wrote: > >Sorry, I didn't notice the "convert all the elements in it to integer" > >part. > >I think I saw reference to the to! before...that is one way to convert. > > > >auto words = file.byLine() // you've all

Re: reading file byLine

2015-09-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Sep 03, 2015 at 11:28:36PM +, Namal via Digitalmars-d-learn wrote: > On Thursday, 3 September 2015 at 23:25:52 UTC, Jordan Wilson wrote: > >And also: > >import std.algorithm > > > >Sorry, I should have taken the time to answer properly and fully. > > import std.file, std.stdio,

Re: reading file byLine

2015-09-03 Thread Jordan Wilson via Digitalmars-d-learn
On Friday, 4 September 2015 at 00:18:15 UTC, Namal wrote: On Thursday, 3 September 2015 at 23:54:44 UTC, H. S. Teoh wrote: [...] Thx Theo, this and the lack of foolproof tutorials were the reason why I gave up on D 2 years ago and went instead to C++. But I am not giving up this time. That

Re: reading file byLine

2015-09-03 Thread deed via Digitalmars-d-learn
On Friday, 4 September 2015 at 01:31:28 UTC, Namal wrote: How can I get just the maximum element? Do I need to give a range for it? Use max? http://dlang.org/phobos/std_algorithm_comparison.html#max

Re: reading file byLine

2015-09-03 Thread Namal via Digitalmars-d-learn
Hope this helps. Yes, it does. I have a question about arrays. I can sort an array A by sort(A); How can I get just the maximum element? Do I need to give a range for it?

Re: reading file byLine

2015-09-02 Thread cym13 via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 15:04:10 UTC, cym13 wrote: On Wednesday, 2 September 2015 at 13:46:54 UTC, Namal wrote: Thx, cym. I have a question about a D strings though. In c++ I would just reuse the string buffer with the "=" how can I clear the string after i store a line in the buffer

Re: reading file byLine

2015-09-02 Thread via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 15:04:10 UTC, cym13 wrote: You can also reset a variable to its initial state (what you would hav had if you hadn't initialized it) using: buffer.init(); Huh? That doesn't work... Instead, use: buffer = buffer.init;

Re: reading file byLine

2015-09-02 Thread Namal via Digitalmars-d-learn
Thx guys, this helped alot. The next thing I want to do is read the file line by line and split the stream into words. I found this example of code that seems to do sort of something like it. How can I modyfy it so I can store the words in an array of strings? Is a => a.length the iterator

Re: reading file byLine

2015-09-02 Thread wobbles via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 21:53:20 UTC, Namal wrote: Thx guys, this helped alot. The next thing I want to do is read the file line by line and split the stream into words. I found this example of code that seems to do sort of something like it. How can I modyfy it so I can store the

Re: reading file byLine

2015-09-02 Thread Namal via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 13:12:39 UTC, cym13 wrote: On Wednesday, 2 September 2015 at 13:01:31 UTC, Namal wrote: Hello, I want to read a file line by line and store each line in a string. I found this example with byLine and ranges. First of all, do I need the range lib at all to do

reading file byLine

2015-09-02 Thread Namal via Digitalmars-d-learn
Hello, I want to read a file line by line and store each line in a string. I found this example with byLine and ranges. First of all, do I need the range lib at all to do this and if so what is the range of the end of the file?

Re: reading file byLine

2015-09-02 Thread cym13 via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 13:01:31 UTC, Namal wrote: Hello, I want to read a file line by line and store each line in a string. I found this example with byLine and ranges. First of all, do I need the range lib at all to do this and if so what is the range of the end of the file?

Re: reading file byLine

2015-09-02 Thread qznc via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 13:46:54 UTC, Namal wrote: On Wednesday, 2 September 2015 at 13:12:39 UTC, cym13 wrote: On Wednesday, 2 September 2015 at 13:01:31 UTC, Namal wrote: Hello, I want to read a file line by line and store each line in a string. I found this example with byLine

Re: reading file byLine

2015-09-02 Thread cym13 via Digitalmars-d-learn
On Wednesday, 2 September 2015 at 13:46:54 UTC, Namal wrote: Thx, cym. I have a question about a D strings though. In c++ I would just reuse the string buffer with the "=" how can I clear the string after i store a line in the buffer and do something with it. I also tried to append a line to