
에서 영감을 받다 수식에 사용된 열을 추출하는 방법이 있나요? 그리고 이안@JMP 논평.
dt = New Table( "Get Columns in Formula",
Add Rows( 20 ),
New Column( "H", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Uniform() ) ),
New Column( "W", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Uniform() ) ),
New Column( "D", Numeric, "Continuous", Format( "Best", 12 ), Formula( Random Uniform() ) ),
New Column( "V", Numeric, "Continuous", Format( "Best", 12 ), Formula( (:W * :H * :D) / 10 ) )
);
formula = dt:V << getformula();
// function to find leaves in formula that are columns in the table
findColumns = Function( {frm, cols},
{h = Head( frm ), n = N Arg( frm ), i, result = {}}, // local variables
If( n == 0, // head node has no children, it is a leaf
If( Contains( cols, Name Expr( h ) ), // is the leaf in the list of columns?
Insert Into( result, Name Expr( h ) ); // yes -> add to result
)
,
For( i = 1, i <= n, i++, // not a leaf, get children's results
Insert Into( result, Recurse( Arg( frm, i ), cols ) )
)
);
result// return the result
);
answer = findColumns( Name Expr( formula ), dt << GetColumnNames );
Show( formula, answer );
공식 = (:W * :H *
) / 10;
답변 = {:W, :H,
};
그만큼 findColumns 10-22행에 정의된 함수는 두 개의 인수를 취합니다. 프롬 , 조사할 수식 및 열 , 추출할 유효한 열 이름 목록입니다.그만큼 머리 예제 표현식 중 두 개의 자식이 있는 Divide(...)입니다(답은 다음과 같습니다). N 인수 ). if 문은 자식이 있는 것을 확인하고 반복하다 각 자식을 처리하는 기능입니다.곱셈 자식에게는 세 명의 자녀가 있습니다.:W 하위에는 하위가 없으며 데이터 테이블의 열 목록에 :W가 포함되어 있으므로 :W는 다음과 같습니다. 에 삽입 목록과 목록이 반환됩니다. 여기서 해당 목록은 목록에 삽입됩니다.그런 다음 곱하기 자식은 두 번째를 보냅니다. 인수 재귀하고 :H가 추가됩니다.결국 10 (디바이드의 둘째 아들) 재귀로 전송되지만 열 목록에 없으므로 답변에 표시되지 않습니다.
재귀에는 매번 약간의 노력이 필요합니다.그러나 그것은 단순해 보이는 프로그램을 만든다.