Convert output of map() to array of strings

Paul Backus snarwin at gmail.com
Wed Aug 15 13:59:40 UTC 2018


On Wednesday, 15 August 2018 at 13:53:02 UTC, Andrey wrote:
> Hello,
> I have the following code:
>> string[] list;
>> string text;
>> // ...
>> enum pattern = ctRegex!`^[0-9]+$`;
>> list = text.split('\n').map!(line => 
>> line.matchFirst(pattern).hit);
>
> Compiler says that it can't convert result of map function to 
> string[]...

The result of `map` is a lazily-evaluated range. To convert it to 
an array, use `std.array.array`:

import std.array: array;
//...
list = text
     .split('\n')
     .map!(line => line.matchFirst(pattern).hit)
     .array;


More information about the Digitalmars-d-learn mailing list