關於動態列公式的一些見解作為一名 JSL 初學者,我發現我經常在腳本解釋方面遇到困難。如果我在討論中發現一個似乎可以解決我的問題的腳本並嘗試對其進行自定義,我經常會破壞它並且很難理解如何修復它。在我的 JSL 腳本編寫之旅中,我非常依賴這個論壇來尋求幫助——既可以在大量檔案中搜尋過去的答案,也可以在答案不明顯時尋求幫助。該論壇是學習 JSL 的寶貴資源,這裡的知識深度和分享意願令人驚訝。
最近,我一直在研究動態列公式,並在幾年前在這裡發現了一篇非常有用的文章。 @txnelson的解決方案和@NisKorsgaard的後續建議對我來說很有啟發。因為我對 JSL 很陌生,所以我經常喜歡「解壓縮」程式碼以弄清楚每個部分的作用。我這樣做是為了解決我遇到的動態列公式問題。這個練習對我非常有幫助,我認為它也可能對其他人有用,所以我在下麵包含了「解壓縮」程式碼。
該腳本沒有執行任何實際操作,但提供瞭如何動態更改列公式的簡單範例。希望有人會發現這很有用。
// CALL COLUMN BY REFERRING TO VARIABLE
// See this Discussion for more info:
// From: https://community.jmp.com/t5/Discussions/call-a-column-by-referring-to-variable/td-p/272339
// Original script from @txnelson
//Names Default To Here( 1 );
//dt = Open( "$SAMPLE_DATA/big class.jmp" );
//columnname1 = Column( dt, 4 ) << Get Name;
//columnname2 = Column( dt, 5 ) << Get Name;
//
//Eval(
// Substitute(
// Expr(
// dt << New Column( "new_" || columnname1,
// Numeric,
// continuous,
// formula( __c1__ + __c2__ )
// )
// ),
// Expr( __c1__ ), Parse( ":" || columnname1 ),
// Expr( __c2__ ), Parse( ":" || columnname2 )
// )
//);
// ==================================================
// REVISED SCRIPT
// Pulls apart the above script a bit. For me, this was easier to visualize
// and understand. Also helped clarify how variables work and why the
// EVAL EXPR function is needed. When running script look at the log for changing
// variable contents.
// SETUP
// **************************************************
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );
columnName1 = Column( dt, 4 ) << Get Name;
columnName2 = Column( dt, 5 ) << Get Name;
// FORMULA WITH PLACEHOLDERS
// Create expression with desired formula using placeholders for each variable and store result in var.
// **************************************************
var = Expr(
dt << New Column( "new_" || columnName1, Formula( __c1__ + __c2__ ) )
);
Show (var); // For learning and debugging. Look in log for value of var.
// SWAP OUT PLACEHOLDERS FOR COLUMN NAME STORED IN VARIABLES
// Replace placeholders with variable contents, make them column names and store result in subVar.
// To see the effect of EVAL EXPR, try substituting EVAL (remove EXPR) and re-running the script.
// Look at the log.
// **************************************************
subVar = Substitute(
Eval Expr( var ),
Expr( __c1__ ), As Name( columnName1 ),
Expr( __c2__ ), As Name( columnName2 )
);
Show( subVar); // For learning and debugging. Look in log for value of subVar.
// DO IT
// Make the column with the new formula.
// **************************************************
Eval( subVar );
自動化和腳本編寫回覆:關於動態列公式的一些見解這是一篇有點舊的文章,但一個很好的起點,使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入到另一個表達式中。當我有足夠的時間時,我會將我對這些(表達式以及在幾種不同情況下的處理)的看法寫成部落格文章。
回覆:關於動態列公式的一些見解@jthi ,感謝有關表達式處理基礎知識的入門課程:
第 9 節:高級 JSL 33 分鐘 - ...
另一個精彩的講座:
使用 JSL 開發高效、強大的應用程式 (EU 2018 415)
Eval(Substitute())是您應該像瑞士軍刀一樣攜帶用於 JSL 腳本編寫的幾個工具之一:)
JMP 中的表達式處理:Tipps 和 Trapdoors
回覆:關於動態列公式的一些見解除了將列名儲存為字串並透過as name將其轉換為「名稱」之外,我們還可以儲存名稱(帶或不帶“:”) - 並透過Name Expr檢索和保存它們:
dt = Open( "$SAMPLE_DATA/big class.jmp" );
col1 = Name Expr(:height);
col2 = Name Expr(:weight);
new name = "new_" || (col1 << get name);
col1 << set name("tmp"); // we don't care - the reference will still point to the right column
var = Expr(
dt << New Column( new name , Formula( __c1__ + __c2__ ) )
);
var = Substitute( Name Expr(var),
Expr( __c1__ ), Name Expr( col1 ),
Expr( __c2__ ), Name Expr( col2 )
);
/*alternative:
Substitute Into( var,
Expr( __c1__ ), Name Expr( col1 ),
Expr( __c2__ ), Name Expr( col2 )
);*/
Eval( var );