出力は次のとおりです。
含む (コリスト、列) = 0;
含む (collist、名前として (colname)) = 1;
Re: 列を参照するこれらの方法で異なる結果が得られるのはなぜですか?この式は列参照を格納しません:
col = AsName("abc");
したがって、Contains() 関数は 0 (false) を返します。列参照を格納する場合は、Column( "name" ) を使用する必要があります。
Re: 列を参照するこれらの方法で異なる結果が得られるのはなぜですか?この先の仮想ウサギの穴。マークの答えがあなたに必要なすべてかもしれません。
魅力的な質問です。確かに同じ答えが得られるはずですが...
C や他の多くの言語とは異なり、関数の引数は関数が呼び出される前に評価されません。代わりに、関数がパラメータの処理方法を決定します。また、JSL には、AsName が返す値と密接に関連する、expression と呼ばれる変数型があります。式引数を指定して関数を呼び出すと、関数はパラメーターをどう扱うかを選択できます。式を評価する場合があります。 a+2 では、変数 a を検索して 2 を加算する必要があります。あるいは、式を操作する可能性があります。 head(a+2) はaddを返します。あなたの例では、式はabcであり、それを調べることは、データテーブルの行からその値を取得することを意味します。
(これは難しいです。コメントを正確かつ明確にするために 1 時間を費やしました。コメントは正しいと思います。)
dt = New Table( "test", New Column( "abc" ), New Column( "def" ) );
collist = {:abc, :def, .}; // <<< added a missing value in position 3
col = As Name( "abc" );// the Right Hand Side evaluates to the expression abc,
// and the expression is assigned to col. The 2nd argument to contains() is
// a name expression, and contains() evaluates that argument. It
// evaluates to missing because there is no subscript to pick a row.
Show( Name Expr( col ), Type( col ), Contains( collist, col ) ); // looks for a missing value
// Name Expr(col) = abc;
// Type(col) = "Number";
// Contains(collist, col) = 3;
colname = "abc";
// contains() evaluates the 2nd argument to a name, but does not
// do another evaluation of the name, so it locates the name
// in the list of names
Show( Type( As Name( colname ) ), Contains( collist, As Name( colname ) ) );
// Type(As Name(colname)) = "Name";
// Contains(collist, As Name(colname)) = 1;
ついに、
Show( Name Expr( col ), Type( col ), Contains( collist, Name Expr(col) ) );
あなたが望むことをします。 contains() の 2 番目の引数には nameexpr() ラッパーがあり、これにより contains() は値ではなく式の名前abcに評価します。
編集:これは決して簡単なことではありません! @hogi さんは、「 ヘッドは引数を評価しますか?」で head(a+2) の例 (上記) が間違っていると指摘しています。 ;答えは、 head() が引数を調べて、それが式を保持する変数であるかどうかを確認し、そうでない場合はそれを評価して式を生成するかどうかを確認することだと思います。
列を参照するこれらの方法で異なる結果が得られるのはなぜですか?JSL が列参照をどのように処理するかを理解するのに苦労しており、そのためスクリプトでバグが発生し続けています。
次の式で異なる結果が得られるのはなぜですか?
列参照を関数に動的に渡す最良の方法は何ですか?
dt = New Table("test",
New Column("abc"),
New Column("def")
);
collist = {:abc, :def};
col = AsName("abc");
colname = "abc";
Show(Contains(collist, col));
Show(Contains(collist, AsName(colname)));出力は次のとおりです。
Contains(collist, col) = 0;
Contains(collist, As Name(colname)) = 1;
自動化とスクリプト作成Re: 列を参照するこれらの方法で異なる結果が得られるのはなぜですか?@Craige_Hales 。
この投稿をありがとう - 私の宝物のリストに追加します:)
なぜかというヒントが得られますName Expr(列(colname)として)
[例: @jthiでRe: 式、列参照、数式、列名の変更についてサポートが必要です』がとても便利です!
私はこう言いたかったのですが、今では本当に理解できたように思います。しかし、以下の結果のいくつかは私にとって依然として驚くべきものです。
私はまだ包括的なドキュメントを探しています - たとえば、どうやって見つけるかなどどうやって関数はそのパラメータを処理します。
経由で検索する場合含まれています、なぜJmpはそれを知っていますか列("abc") = :abc?
dt = New Table( "test", New Column( "abc" ), New Column( "def" ) );
collist = {:abc, :def, .}; // <<< added a missing value in position 3
row()=0; //to be sure that the row index is really 0 :)
colname = "abc";
col = As Name( colname); // abc
col2 = :name("abc"); // .
col3 = As Column(colname); // .
col4 = Name Expr(As Column(colname)); // :abc
col5 = Column(colname); // Column("abc")
col6 = Name Expr(Column(colname)); // Column( colname )
col; // .
col2; // .
col3; // .
col4; // .
col5; // Column( "abc" )
col6; // Column( "abc" )
Show( Name Expr( col ), Type( col ), Contains( collist, col ) ); // :abc, Number, 3
Show( Name Expr( col2 ), Type( col2 ), Contains( collist, col2 ) ); // . , Number, 3
Show( Name Expr( col3 ), Type( col3 ), Contains( collist, col3 ) ); // ., Number, 3
Show( Name Expr( col4 ), Type( col4 ), Contains( collist, col4 ) ); // :abc, Number, 3
Show( Name Expr( col5 ), Type( col5 ), Contains( collist, col5 ) ); // Column("abc"), Column, 1 (!)
Show( Name Expr( col6 ), Type( col6 ), Contains( collist, col6 ) ); // Column(colname), Column, 1 (!!)
Show( Contains( collist, Name Expr(col) ) ); // 1
Show( Contains( collist, Name Expr(col2) ) ); // 3
Show( Contains( collist, Name Expr(col3) ) ); // 3
Show( Contains( collist, Name Expr(col4) ) ); // 1
Show( Contains( collist, Name Expr(col5) ) ); // 1
Show( Contains( collist, Name Expr(col6) ) ); // 0