关于动态列公式的一些见解作为一名 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
回复:关于动态列公式的一些见解除了将列名存储为字符串并通过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 );