具有如下片段:如何在映射和拆分之後將地圖映射到特定元素?
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))))
替換'.map!(a => splitter(a,「:」)。 )''用'.map!(a => a.until(':'))'不知何故地弄亂我的結果:我得到未排序的數據(同時給予排序作爲輸入)。 – Patryk
這可能是由於byLine的結果不穩定造成的,您可以嘗試'a => a.idup.until(':')' – weltensturm
相同:/完整代碼段https://dpaste.dzfl.pl/d2cc511bd7cb – Patryk