映射和拆分后如何将映射带到特定元素?
How to take map to particular element after map and split?
具有以下代码段:
import std.algorithm;
import std.array : split;
import std.stdio;
import std.file;
import std.range;
void main(string[] args)
{
string filename = "file.log";
string term = "action";
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => splitter(a, ":"));
// now how to take only first part of split? up to first ':'?
foreach (line; results)
writeln(line);
}
我只对拆分操作后的第一部分感兴趣(或其他一些可能更有效的操作 - 只需先找到 :
并提取它之前的所有字符)。
我试过类似的东西:
.map!(a => a[0])
拆分后出现错误
main.d(37): Error: no [] operator overload for type Result
/usr/include/dmd/phobos/std/algorithm/iteration.d(488): instantiated from here: MapResult!(__lambda4, MapResult!(__lambda3, FilterResult!(__lambda2, ByLine!(char, char))))
main.d(37): instantiated from here: map!(MapResult!(__lambda3, FilterResult!(__lambda2, ByLine!(char, char))))
你可以使用
std.algorithm.findSplitAfter
:
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => a.findSplitAfter(":")[1]);
另一个选项结合了 find
to get you to the :
and drop
让你通过它:
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => a.find(":").drop(1));
看来我也可以用splitter.front
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => splitter(a, ":").front);
(知道除了 front
之外是否还有索引运算符之类的东西吗?)
使用until.
.map!(a => a.until(':'));
group
的默认比较是a == b
,这对懒惰的Untils不起作用。要将它与 group
一起使用,您需要传递有效的比较,即 equal:
.map!(a => a.until(':'))
.group!equal
...
具有以下代码段:
import std.algorithm;
import std.array : split;
import std.stdio;
import std.file;
import std.range;
void main(string[] args)
{
string filename = "file.log";
string term = "action";
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => splitter(a, ":"));
// now how to take only first part of split? up to first ':'?
foreach (line; results)
writeln(line);
}
我只对拆分操作后的第一部分感兴趣(或其他一些可能更有效的操作 - 只需先找到 :
并提取它之前的所有字符)。
我试过类似的东西:
.map!(a => a[0])
拆分后出现错误
main.d(37): Error: no [] operator overload for type Result
/usr/include/dmd/phobos/std/algorithm/iteration.d(488): instantiated from here: MapResult!(__lambda4, MapResult!(__lambda3, FilterResult!(__lambda2, ByLine!(char, char))))
main.d(37): instantiated from here: map!(MapResult!(__lambda3, FilterResult!(__lambda2, ByLine!(char, char))))
你可以使用
std.algorithm.findSplitAfter
:
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => a.findSplitAfter(":")[1]);
另一个选项结合了 find
to get you to the :
and drop
让你通过它:
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => a.find(":").drop(1));
看来我也可以用splitter.front
auto results = File(filename, "r")
.byLine
.filter!(a => canFind(a, term))
.map!(a => splitter(a, ":").front);
(知道除了 front
之外是否还有索引运算符之类的东西吗?)
使用until.
.map!(a => a.until(':'));
group
的默认比较是a == b
,这对懒惰的Untils不起作用。要将它与 group
一起使用,您需要传递有效的比较,即 equal:
.map!(a => a.until(':'))
.group!equal
...