www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - File .. byLine

reply Joel <joelcnz gmail.com> writes:
I can't seem to get this to work!

```
foreach(line; File("help.txt").byLine) {
     writeln(line.stripLeft);
```

With the code above, I get this compile error:
source/app.d(360,36): Error: template 
std.algorithm.mutation.stripLeft cannot deduce function from 
argument types !()(char[]), candidates are:
/usr/local/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(2602,7):       
std.algorithm.mutation.stripLeft(Range, E)(Range range, E element) if
(isInputRange!Range && is(typeof(range.front == element) : bool))
/usr/local/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(2610,7):       
std.algorithm.mutation.stripLeft(alias pred, Range)(Range range) if
(isInputRange!Range && is(typeof(pred(range.front)) : bool))

I just want to use each 'line' variable as a string?! I've tried 
'byLineCopy', and 'line.to!string'. I've looked at documentation.
Dec 02 2018
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 3 December 2018 at 06:09:21 UTC, Joel wrote:
 I can't seem to get this to work!

 ```
 foreach(line; File("help.txt").byLine) {
     writeln(line.stripLeft);
 ```

 With the code above, I get this compile error:
 source/app.d(360,36): Error: template 
 std.algorithm.mutation.stripLeft cannot deduce function from 
 argument types !()(char[]), candidates are:
 /usr/local/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(2602,7):       
std.algorithm.mutation.stripLeft(Range, E)(Range range, E element) if
(isInputRange!Range && is(typeof(range.front == element) : bool))
 /usr/local/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(2610,7):       
std.algorithm.mutation.stripLeft(alias pred, Range)(Range range) if
(isInputRange!Range && is(typeof(pred(range.front)) : bool))

 I just want to use each 'line' variable as a string?! I've 
 tried 'byLineCopy', and 'line.to!string'. I've looked at 
 documentation.
https://run.dlang.io/is/h0ArAB works for me. If you want it as a string not char[] then byLineCopy should work, if not just `.idup` `line`.
Dec 02 2018
parent reply Joel <joelcnz gmail.com> writes:
On Monday, 3 December 2018 at 06:55:50 UTC, Nicholas Wilson wrote:
 On Monday, 3 December 2018 at 06:09:21 UTC, Joel wrote:
 [...]
https://run.dlang.io/is/h0ArAB works for me. If you want it as a string not char[] then byLineCopy should work, if not just `.idup` `line`.
Oh, I was using std.algorithm's 'stripLeft' instead of std.string's 'stripLeft'.
Dec 02 2018
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/3/18 2:03 AM, Joel wrote:
 On Monday, 3 December 2018 at 06:55:50 UTC, Nicholas Wilson wrote:
 On Monday, 3 December 2018 at 06:09:21 UTC, Joel wrote:
 [...]
https://run.dlang.io/is/h0ArAB works for me. If you want it as a string not  char[] then byLineCopy should work, if not just `.idup` `line`.
Oh, I was using std.algorithm's 'stripLeft' instead of std.string's 'stripLeft'.
Interesting. I can see the reason too -- there is no "should be stripped" property for all types, just character types (is it a space). -Steve
Dec 03 2018