Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 01, 2014 std.array.array broken? | ||||
---|---|---|---|---|
| ||||
--- import std.stdio; import std.array; auto lines = File(filename).byLine.array; writeln(lines); // Crap --- dmd 2.064(.2 I think) Docs say: - std.stdio.byLine returns an input range - std.array.array takes an input range |
February 01, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to deed | On Saturday, 1 February 2014 at 22:47:54 UTC, deed wrote:
> Docs say:
> - std.stdio.byLine returns an input range
> - std.array.array takes an input range
Docs also say:
/**
Note:
Each $(D front) will not persist after $(D
popFront) is called, so the caller must copy its contents (e.g. by
calling $(D to!string)) if retention is needed.
*/
So you need to do a duplication for each element. Use this code:
-----
import std.stdio;
import std.array;
import std.algorithm;
void main()
{
auto lines = File("test.d").byLine.map!(a => a.dup).array;
writeln(lines);
}
-----
|
February 01, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Saturday, 1 February 2014 at 22:52:24 UTC, Andrej Mitrovic wrote:
> On Saturday, 1 February 2014 at 22:47:54 UTC, deed wrote:
>> Docs say:
>> - std.stdio.byLine returns an input range
>> - std.array.array takes an input range
>
> Docs also say:
>
> /**
> Note:
> Each $(D front) will not persist after $(D
> popFront) is called, so the caller must copy its contents (e.g. by
> calling $(D to!string)) if retention is needed.
> */
>
> So you need to do a duplication for each element. Use this code:
>
> -----
> import std.stdio;
> import std.array;
> import std.algorithm;
>
> void main()
> {
> auto lines = File("test.d").byLine.map!(a => a.dup).array;
> writeln(lines);
> }
> -----
so array copy the copied fronts?
|
February 01, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to deed | deed:
> auto lines = File(filename).byLine.array;
> writeln(lines); // Crap
> ---
Beside the answers that others have already given you, another way to do that is to read the whole file (with read or readText) and then use splitlines on the string.
Bye,
bearophile
|
February 01, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 1 February 2014 at 22:59:12 UTC, bearophile wrote:
> deed:
>
>> auto lines = File(filename).byLine.array;
>> writeln(lines); // Crap
>> ---
>
> Beside the answers that others have already given you, another way to do that is to read the whole file (with read or readText) and then use splitlines on the string.
>
> Bye,
> bearophile
Thanks
|
February 01, 2014 Re: std.array.array broken? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Saturday, 1 February 2014 at 22:52:24 UTC, Andrej Mitrovic wrote:
> On Saturday, 1 February 2014 at 22:47:54 UTC, deed wrote:
>
> Docs also say:
>
> /**
> Note:
> Each $(D front) will not persist after $(D
> popFront) is called, so the caller must copy its contents (e.g. by
> calling $(D to!string)) if retention is needed.
> */
>
> So you need to do a duplication for each element. Use this code:
Sorry, but I really don't understand this. At all.
"Returns a newly-allocated dynamic array consisting of a copy of the input range" to me sounds "Hey, I'm doing a copy". How is one supposed to understand that you need to go through hops for getting a good result?
|
Copyright © 1999-2021 by the D Language Foundation