수식에 누락된 열이 있을 때 오류를 잡습니다. 다음 예에서 테이블에는 열이 2개 있습니다. 존재하지 않는 열을 사용하는 새 열을 추가하면 결과에 결과가 누락되고(올바르게) try 문이 작동하지 않아 잡을 수 없는 스크립트를 차단하는 오류가 발생합니다. 이에 대한 해결 방법이 있습니까?
dt= New Table( "Test",
Add Rows( 1 ),
New Column( "A",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [2] )
),
New Column( "B",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Selected,
Set Values( [1] )
)
);
formula=":Name(\!"C\!") - :Name(\!"B\!")";
try(
eval(parse("dt << new column(\!"value\!", Numeric, \!"Continuous\!", Formula(" ||formula || "))"));
,
error="missing column";
);
열이 누락된 경우 최종 결과가 무엇인지 정확히 알 수 없지만 아래는 문제를 처리하는 한 가지 방법입니다.
dt = New Table( "Test",
Add Rows( 1 ),
New Column( "A",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [2] )
),
New Column( "B",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Selected,
Set Values( [1] )
)
);
If( Try( dt:c << get name, "" ) == "",
type = "character";
formula = "\!"missing column\!"";
,
type = "numeric/continuous";
formula = ":Name(\!"C\!") - :Name(\!"B\!")";
);
Eval(
Parse(
"dt << new column(\!"value\!"," || type || ", Formula(" || formula
|| "))"
)
);감사합니다만 별로 유용하지 않습니다. 문제는 자동으로 일부 데이터(스크립트로)를 추출하고 그 중 하나가 누락될 수 있다는 것입니다(그러나 사전에 어떤 경우인지 알 수 없음). 누락된 열이 발견되면 스크립트가 중지되며 이것이 실제 문제입니다. 좋은 해결책은 try 문일 수 있지만 작동하지 않습니다.
수식 열을 만들기 전에 테스트하는 것이 더 나을 수 있습니다. 따라서 if 문을 수행하여 열이 존재하는지 확인합니다... 없으면 수식 열을 만들지 마십시오.
@antonio-도메닉 , 원하는 것은 수식을 기반으로 열을 만들기 전에 테이블에 주어진 수식에 대한 적절한 열이 포함되어 있는지 확인하는 것입니다.열을 만들기 전에 이것을 결정할 수 있다면 스크립트 중지 오류가 발생할 가능성이 줄어듭니다.
좋은 소식은 공식이 의존하는 모든 외부 이름을 결정하는 것이 매우 간단하다는 것입니다. 이 이름은 현재 데이터 테이블의 열 이름이어야 합니다.(수식이 현재 범위 내 변수에 의존하지 않는 것이 가장 좋습니다. 이 변수는 미래의 어느 시점에서 범위를 잃어 수식에 오류가 발생하게 됩니다.)
다음은 매달린 이름에 대해 표현식을 재귀적으로 구문 분석하는 함수가 포함된 몇 가지 예제 코드입니다(공식은 물론 표현식일 뿐입니다).그런 다음 테이블의 열 이름을 확인하고 열 이름이 공식을 충족하는 경우 적용할 수 있습니다.
Names Default to Here( 1 );
dt= New Table( "Test",
Add Rows( 1 ),
New Column( "A",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [2] )
),
New Column( "B",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Selected,
Set Values( [1] )
)
);
//recursively find all unassigned names within the formula -- we can test these against the column names
get dangling names = Function( {formula},
{Default Local},
parse formula = Function( {expr, names = {}, assigns = {}},
{Default Local},
If( Contains( Head Name( expr ), "[" ) & Contains( Head Name( expr ), "]" ), // deal with the exceptional case where JMP doesn't arg-parse out this pattern...
items = Words( Head Name( expr ), "[" );
Summation( i = 1, N Items( items ),
Try(
item = Word( 1, items[i], "]" );
If( Is Missing( Num( item ) ),
{n, a} = Recurse( Parse( item ) );
Insert Into( names, n );
Insert Into( assigns, a );
)
);<BR /> 0
)
,
If( Head Name( expr ) == "Assign", // if a variable is assigned within the formula then we don't care to check it (the formula shouldn't be direclty affecting other columns, it bad practice!)
Insert Into( assigns, Char( Arg( expr, 1 ) ) )
,
Head Name( expr ) != Char( Name Expr( expr ) ), // Need to recursively parse here
Summation( i = 1, N Arg( expr ),
{n, a} = Recurse( Arg( expr, i ) );
Insert Into( names, n );
Insert Into( assigns, a );
0
);
,
Insert Into( names, Char( Name Expr( expr ) ) ) // found a name
);
);
Eval List( {Associative Array( names ) << Get Keys, Associative Array( assigns ) << Get Keys} )
);
{names, assigns} = parse formula( Name Expr( formula ) );
names = Associative Array( names );
assigns = Associative Array( assigns );
names << Remove( assigns );
names = names << Get Keys;
names
);
formulas = {};
Insert Into( formulas, Expr( :Name("C") - :Name("B") ) );
local var = [1 => 3, 2 => 4];
Insert Into( formulas, Eval Expr(
var = Expr( local var );
var[:C][:F] + :D
);
);
Insert Into( formulas, Expr( :A + :B ) );
Insert Into( formulas, Expr( :A + :B - Sin( :D</img> + Cos( :E - :Name("flamingo@fish" ) ) ) ) );
For( i = 1, i <= N Items( formulas ), i++,
table columns = dt << Get Column Names( "String" );
Write( "\!N" );
Show( table columns );
formula references = get dangling names( formulas[i] );
refs = {};
For( j = 1, j <= N Items( formula references ), j++,
refs[j] = Contains( table columns, formula references[j] ) > 0
);
Show( formulas[i] );
Show( formula references );
Print( If( Sum( refs ) == N Items( formula references ), "OKAY", "NOT OKAY" ) );
If( Sum( refs ) == N Items( formula references ), // The table contains all the necessary columns, okay to add the new column!
Eval( Eval Expr(
dt << New Column( "FORMULA", <<FORMULA></FORMULA>질문이 있으면 알려주세요!
요르단,