使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式問題您需要插入變數的值變成一個表達式而不評估整個代碼。範例包括:
- 使用腳本編寫列公式
- 使用另一個腳本編寫腳本
下面是一個用於轉換表中選定列的腳本。 這確實不是有效,因為表達式“cols[i]”保存在列公式中,而不是列名稱中。
//Open A Table and select a few columns
dt = Open( "$Sample_data/iris.jmp" );
cols = {Sepal length, Petal length};
//This code does not work
For( i = 1, i <= N Items(cols), i++,
dt << New Column(
"2 * " || Char( cols[i] ),
Numeric,
"Continuous",
Formula( As Column( cols[i] ) * 2)
)
);
使用正確的名稱建立新列,但公式如下:

解決方案
有兩種通用策略可以解決此問題:建立表達式,同時在其餘部分之前評估其中的一部分;並將表達式建構為字串,然後評估它。 請注意,插入的變數實際上可以是任意長度的表達式。
表達式
這些方法涉及編寫表達式並在其餘部分之前評估其中的一部分。 找到 Eval、Eval Expr 和 Expr 的正確組合可能很棘手,但您可以看到語法突出顯示並使用腳本編輯器來確保括號匹配。
評估,評估表達式
編寫表達式並包裝應使用 Expr() 立即計算的任何部分:
//Eval Expr
For( i = 1, i <= N Items(cols), i++,
Eval( Eval Expr(
dt << New Column(
"2 * " || Char( cols[i] ),
Numeric,
"Continuous",
Formula( As Column( Expr( Char( cols[i] ) ) ) * 2)
)
) )
);
代替
編寫表達式並用計算程式碼取代其中的一部分:
//Substitute
For( i = 1, i <= N Items(cols), i++,
Eval( Substitute(
Expr( dt << New Column(
"2 * " || cn,
Numeric,
"Continuous",
Formula( As Column( cn ) * 2)
) ),
Expr( cn ),
Char( cols[i] )
) )
);
字串方法
這些方法涉及將表達式編寫為字串,使用 Parse() 將其轉換為表達式,然後使用 Eval() 對其進行實際評估。 這些方法的缺點包括看不到任何語法突出顯示,需要使用 \!" 或 \[ ... ]\ 處理雙引號,並且將來通常很難排除故障或修改,尤其是對於長表達式。
評估插入物
將表達式編寫為字串,並使用“內聯”程式碼進行計算並將其放置在字串內,然後解析併計算它:
//Eval Insert
For( i = 1, i <= N Items(cols), i++,
Eval( Parse( Eval Insert(
"\[dt << New Column(
"2 * ^Char( cols[i] )^",
Numeric,
"Continuous",
Formula( As Column( "^Char( cols[i] )^" ) * 2)
)]\"
) ) );
);
代替
將表達式寫入帶有佔位符的字串中,該佔位符將替換為計算的表達式。 請注意,這與上面的函數相同:
//Substitute
For( i = 1, i <= N Items(cols), i++,
Eval( Parse( Substitute(
"\[dt << New Column(
"2 * ColumnName",
Numeric,
"Continuous",
Formula( As Column( "ColumnName" ) * 2)
)]\",
"ColumnName",
Char( cols[i] )
) ) );
);
連接
最基本的形式,使用“||”來寫表達式。請注意,您可以使用 \[ ... ]\ 而不是 \!":
//Concatenate
For( i = 1, i <= N Items(cols), i++,
Eval( Parse(
"dt << New Column(
\!"2 * " || Char( cols[i] ) || "\!",
Numeric,
\!"Continuous\!",
Formula( As Column( \!"" || Char( cols[i] ) || "\!" ) * 2)
)"
) );
);
也可以看看
將變數或表達式插入到其他表達式中。這是 Eval()、Eval Expr()、Substitute()、Parse() 和 Eval Insert() 的簡要介紹。
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式以下是表達式方法和字串方法的範例:
Names default to here(1);
//Open A Table and select a few columns
dt = Open( "$Sample_data/iris.jmp" );
cols = {Sepal length, Petal length};
//Eval Expr
For( i = 1, i <= N Items(cols), i++,
Eval( Eval Expr(
dt << New Column(
"Eval Expr 2 * " || Char( cols[i] ),
Numeric,
"Continuous",
Formula( Expr( cols[i] ) * 2 )
)
) )
);
//Substitute with String
For( i = 1, i <= N Items(cols), i++,
Eval( Parse( Substitute(
"\[dt << New Column(
"Substitute String 2 * ColumnName",
Numeric,
"Continuous",
Formula( :"ColumnName"n * 2)
)]\",
"ColumnName",
Char( cols[i] )
) ) );
);
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式這可能有一些不必要的功能,但您可以將 Name Expr 與列引用一起使用(As Column(name)):
Names Default To Here(1);
//Open A Table and select a few columns
dt = Open("$Sample_data/iris.jmp");
cols = {Sepal length, Petal length};
For(i = 1, i <= N Items(cols), i++,
Eval(Eval Expr(dt << New Column("2 * " || Char(cols[i]), Numeric, "Continuous", Formula(Expr(Name Expr(As Column(Char(cols[i])))) * 2))))
);
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式有沒有一種方法不需要 As Column() ?
在這裡,我們需要在傳遞字串時包含它,我想知道如何做到這一點,以便公式保留 :column。
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式多謝! 是的。 你對循環的開始是對的。 我沒有意識到這一點。
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式@新手,
for 迴圈從 i=2 開始,這就是它跳過第一列的原因,並且在建立資料表時建立了一個名為「Column 1」的空白列。 您可以透過以 i=1 啟動循環並在之後刪除第 1 列來「修復」此問題,如下所示:
看更多... 回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式嗨,呃,
它適用於 b 列和 c 列。 “a”沒有出現。 相反,會出現一個空的 Column 1。
這就是為什麼我嘗試將「for 迴圈」放入 New Table() 內。 但透過這種方法,我只得到公式內的表達式。
您知道如何使用 New Table() 內的公式處理「for 迴圈」以便一次取得所有必要的欄位嗎?
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式你好@新手,
您需要告訴 JMP 在將該表達式放入公式之前對其進行計算,因為一旦進入列公式,它就不再知道如何引用您的其他表。 使用我喜歡的方法(上面的方法),可能看起來像這樣:
Names Default To Here( 1 ); //
//Table with columns you want to create
mydt = New Table( "Source",
Add Rows( 3 ),
New Column( "Name", Character, "Nominal", Set Values( {"a", "b", "c"} ) ),
New Column( "Mean", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, 2, 3] ) ),
New Column( "Std Dev", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [0.5, 0.6, 0.7] ) )
);
my_newdt = New Table( "Target", Add Rows( 100 ) );
// For loop. Add Columns
For( i = 2, i <= N Rows( mydt ), i++, //
Eval( Eval Expr(
my_newdt << New Column( Column( mydt, 1 )[i],
Numeric,
"Continuous",
Format( "Best", 6 ),
Formula( Random Normal( Expr( As Column( mydt, :Mean )[i] ), Expr( As Column( mydt, :Std Dev )[i] ) ) )//
)
) )
);
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式建立一個表,其中列中包含公式 - Random Normal() - 來自外部 jmp
我已經為我的問題找到了可行的解決方案。 鏈接如上。
然而,我非常想讓我的程式碼正常運作。
這個帖子似乎回應了我的主題。
我嘗試了上述一些技術。 但這不起作用或我不明白。
你有什麼建議給我嗎? 如何獲得表達“作為列(mydt,:平均值)[1] 「決心尋找來源值?
//not working
Clear Globals();
Names Default To Here( 1 ); //
mydt = Current Data Table();
// New data table
my_newdt = New Table( "Table Name",
// Add Rows( N Rows( mydt ) ), // row count can be set by hand e.g. 1000
Add Rows( 10 ), // row count can be set by hand e.g. 1000
New Column( Column( mydt, 1 )[1], // Loop i==1
Numeric,
"Continuous",
Format( "Best", 6 ),
Formula( Random Normal( As Column( mydt, :Mean )[1], 0.01))// As Column(mydt, :Mean )[1] is evalueted in the target table. But Formula has the expression "As Column( mydt, :Mean )[1]" instead of a value from source table
)
);// End New data table
/*// For loop. Add Columns
For( i = 2, i <= N Rows( mydt ), i++, //
my_newdt << New Column( Column( mydt, 1 )[i], // Loop i ==2
Numeric,
"Continuous",
Format( "Best", 6 ),
Set Each Value( Random Normal( As Column( mydt, :Mean )[i], As Column( mydt, :Std Dev )[i] ) )//
)
)// End for loop;
;*/
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式很有用!
回覆:使用 Eval Insert、Eval Expr、Parse 和 Substitute 將一個表達式插入另一個表達式對可用於動態建構 JSL 表達式的方法進行了精彩回顧。掌握了這一點,一切就相當簡單了。