내가 얻고 싶은 연관 배열의 예는 다음과 같습니다.
string [string] rlist = ["dima":"first", "masha":"second", "roma":"third"];
내가 읽은 텍스트 파일은 매우 간단한 구조를 가지고 있습니다.
peter = fourth ivan = fifth david = sixth
string [string] strarr;
string txt = readText("test.txt");
foreach (t;txt.splitLines())
{
// ??
}
아무도 방법을 제안 할 수 있습니까?
나 일지 모르지만 for 루프와 임시 변수로 추론하기가 어렵습니다. 차라리 다음과 같이하고 싶습니다.
import std.conv;
import std.stdio;
import std.array;
import std.algorithm;
void main() {
string[string] dic = File("test")
.byLine
.map!(l => l.to!string.findSplit(" = "))
.map!(l => tuple( l[0], l[2] ))
.assocArray;
}
byLine : 전체를 읽은 다음 분할하는 것보다 한 줄씩 읽습니다.
첫 번째 맵 : rcorre에서 설명한대로 각 선을 세 부분으로 분할
두 번째 맵 : 분할 된 라인에서 쌍을 만듭니다.
assocArray : 해당 쌍에서 연관 배열을 만듭니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다