輸出是:
包含(collist,col)= 0;
包含(collist,作為名稱(colname))= 1;
回复:為什麼這些引用列的方式會給出不同的結果?此表達式不存儲列引用:
col = AsName("abc");
所以 Contains() 函數返回 0(假)。如果要存儲列引用,則應使用 Column( "name" )。
回覆:為什麼這些引用列的方式會給出不同的結果?前方的虛擬兔子洞。馬克的答案可能就是你所需要的。
這是一個令人著迷的問題。看起來你應該得到相同的答案,但是...
與 C 和許多其他語言不同,函數的參數在呼叫函數之前不會被求值。相反,函數決定如何處理其參數。並且,JSL 有一個稱為表達式的變數類型,與 AsName 傳回的值密切相關。當您使用表達式參數呼叫函數時,函數可以選擇如何處理該參數。它可能會評估表達式; a+2 需要找出變數 a 並新增 2。否則它可能會操縱表達式; head(a+2) 返回add 。在您的範例中,表達式為abc ,查找它意味著從資料表中的行取得其值。
(這很難,我花了一個小時試圖使評論正確且清晰。我認為它們是正確的。)
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() 的第二個參數具有 nameexpr() 包裝器,該包裝器導致 contains() 將其計算為表達式的名稱abc ,而不是值。
編輯:這絕非易事! @hogi指出 head(a+2) 範例(上面)在《Head 評估其參數嗎?》中是錯誤的。 ;我認為答案是 head() 查看參數以查看它是否是保存表達式的變量,如果不是,則對其求值以查看它是否會生成表達式。
回复:為什麼這些引用列的方式會給出不同的結果?@Craige_Hales .
謝謝你的這篇文章——我會把它添加到我的珍寶清單中:)
它給出了一些提示為什麼名稱 Expr(As Column(colname))
[例如通過@jthi在回复:需要有關表達式、列引用、公式和列重命名的幫助】 太有用了!
我想說:我想我現在真的明白了——但下面的一些結果仍然讓我感到驚訝。
我仍在尋找全面的文檔 - 例如如何找出如何一個函數處理它的參數。
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
為什麼這些引用列的方式會給出不同的結果?我正在努力理解 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;
自動化和腳本編寫