중첩 루프를 사용하여 동적 변수 이름을 생성하는 문제에 관하여동적 변수 문제와 관련하여 아래 JSL을 참고해 주세요. 과일과 월을 결합한 변수 이름(예: Apple_Jun)을 생성하고 싶습니다. 하지만 다음 JSL이 실행되지 않습니다. 어떻게 해결해야 할까요?
FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};
For( count = 1, count <= N Items( FruitList ), count++,
For( countM = 1, countM <= N Items( MonthList ), countM++,
ex = Expr(
Parse( Fruit || Month ) = 1;
);
Substitute Into( ex,
Expr( Fruit ), FruitList[Count],
Expr( Month ),
MonthList[CountM]
);
);
Eval( ex );
);
Show( Apple_Jan );
자동화 및 스크립팅Re: 중첩 루프를 사용하여 동적 변수 이름을 생성하는 문제에 관하여당신은 JSL을 필요한 것보다 훨씬 더 복잡하게 만들었습니다.
Names Default To Here( 1 );
FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};
ColNamesList = {};
For Each( {Month}, MonthList,
For Each( {Fruit}, FruitList,
Insert Into( ColNamesList, Fruit || Month )
)
);
Show( ColNamesList );
Re: 중첩 루프를 사용하여 동적 변수 이름을 생성하는 문제에 관하여
Re: 중첩 루프를 사용하여 동적 변수 이름을 생성하는 문제에 관하여Parse()가 필요한 몇 안 되는 장소 중 하나이며 표현식 처리를 통해 더 쉬운 접근 방식은 없는 듯합니다.
변수 이름 생성.
반면에:
이런 경우의 99%에서는 한 단계 더 멀리까지 가서 주요 목표에 대한 더 쉬운 접근 방법을 찾을 수 있습니다.
사용자 정의 변수 이름에 의존하지 않는 것입니다.
예를 들어 2차원 배열, 중첩된 목록 또는 연관 배열의 연관 배열을 사용합니다.
과일_월
그리고 인덱싱과 같은
과일_월[i,j], (과일_월[i])[j] 또는 (과일_월[월])[과일]
변수 이름의 "2D 배열" 대신.
Re: 중첩 루프를 사용하여 동적 변수 이름을 생성하는 문제에 관하여동적 변수 이름을 생성할 수 있는 경우는 매우 제한적이며, 그런 경우에도 일반적으로 그렇게 해서는 안 됩니다. 대신 적절한 데이터 구조(테이블, 연관 배열 , 리스트 등)를 사용하는 것이 좋습니다.
연관 배열을 사용한 예
Names Default To Here(1);
FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};
aa = Associative Array(FruitList, Repeat({Associative Array(MonthList, As List(Repeat(1, N Items(MonthList))))}, N Items(FruitList)));
aa = Associative Array();
For Each({fruit}, fruitlist,
For Each({month}, monthlist,
If(!Contains(aa, fruit),
aa[fruit] = Associative Array();
);
aa[fruit][month] = 1;
);
);
Show(aa["Apple"]["_Jan"]);
읽기 어려운 버전
Names Default To Here(1);
FruitList = {"Apple", "Banana", "Cherry", "Durian", "Entawak"};
MonthList = {"_Jan", "_Feb", "_March", "_April", "_May"};
aa = Associative Array(FruitList, Repeat({Associative Array(MonthList, As List(Repeat(1, N Items(MonthList))))}, N Items(FruitList)));
Show(aa["Apple"]["_Jan"]);