Während ich an einem großen Skript arbeitete, fiel mir ein interessantes Verhalten in Bezug auf Ausdrücke auf.
Betrachten Sie den folgenden JSL-Codeabschnitt:
<FONT color="#339966">// Create a namespace for cleanliness</FONT>
ns = New Namespace("ns");
<FONT color="#339966">// Make an example data table dt</FONT>
ns:dt = <FONT color="#3366FF">New Table</FONT>("window",
<FONT color="#3366FF">New Column</FONT>("a", set values([1,2,3])),
<FONT color="#3366FF">New Column</FONT>("b", set values([1,4,9]))
);
<FONT color="#339966">// 1. Make an expression gr_exp1 containing the graph builder instructions</FONT>
ns:gr_exp1 = <FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << <FONT color="#0000FF">Graph Builder</FONT>(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);<BR /><FONT color="#339966">// Running the expression will build the graph (as expected)
<FONT color="#000000">ns:gr_exp1;</FONT><BR />
// 2. Make a function f1 that saves the graph builder instructions into an expression gr_exp2</FONT>
ns:f1 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:gr_exp2 = <FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << <FONT color="#0000FF">Graph Builder</FONT>(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);
);
<FONT color="#339966">// When one runs the function, the graph will be displayed, though only saving the <BR />// expression into a variable is desired.</FONT>
ns:f1;Da ich für dieses Skript das Diagramm nur als Ausdruck speichern (und später auswerten) wollte, wollte ich nicht, dass es als Popup angezeigt wird. Nach einigem Ausprobieren stellte ich fest, dass das Verschachteln des Ausdrucks in einen anderen Ausdruck das Problem löste:
ns:f2 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:gr_exp2 = <FONT color="#3366FF">expr</FONT>(<FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
));
);<BR /><FONT color="#339966">// Now no graph is displayed when running the function, and the expression is saved!</FONT><BR />ns:f2;<BR />Damit war mein Problem gelöst. Aber aufgrund der Struktur meines Skripts musste ich die Funktion f2 eigentlich von einer anderen Funktion aus aufrufen, beispielsweise f3:
<FONT color="#339966">// 4. Now make a function f3 that calls f2 (with the idea of simply saving an expression)</FONT>
ns:f3 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:f2;
);
<FONT color="#339966">// Running the script will produce a graph again...</FONT>
ns:f3;Dann bemerkte ich, dass das Problem durch die Änderung der Funktion f2 so gelöst wurde, dass sie einen dreifach verschachtelten Ausdruck enthält – dadurch wird der Graph als Ausdruck gespeichert, während seine Auswertung unterdrückt wird:
<FONT color="#339966">// This can be solved by modifying f2</FONT>
ns:f2 = <FONT color="#3366FF">function</FONT>({},{Default Local},
ns:gr_exp2 = <FONT color="#3366FF">expr</FONT>(<FONT color="#3366FF">expr</FONT>(<FONT color="#3366FF">expr</FONT>(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( :p</img> ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
)));
);
<FONT color="#339966">// Running f3 now acts as desired (saves expression and suppresses evaluation of graph)</FONT>
ns:f3;Ich habe mich zwar durch dieses Problem navigieren können, meine Frage ist jedoch:
Wie erstelle ich (richtig) eine Funktion, die beim Aufruf den Ausdruck in einer Variablen speichert , ohne den Ausdruck auszuwerten?
Ich verwende JMP Pro 18.0. Jede Hilfe oder Kommentare sind willkommen.
Hallo @ThomasB ,
Bitte beachten Sie, dass der letzte Ausdruck einer Funktion als Rückgabewert verwendet wird.
Wenn Sie den Ausdruck nur zuweisen möchten, ohne ihn „zurückzugeben“, fügen Sie bitte einen Dummy-Rückgabewert hinzu:
ns:f1 = function({},{Default Local},
ns:gr_exp2 = expr(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);
1;
);oder
ns:f1 = function({},{Default Local},
ns:gr_exp2 = expr(ns:gr = ns:dt << Graph Builder(
Show Control Panel( 0 ),
Variables( X( :a ), Y( ) ),
Elements( Points( X, Y, Legend( 8 ) ), Smoother( X, Y, Legend( 9 ) ) ));
);
return(1);
);
Vielen Dank für deine Antwort und den Link, Jarmo, sie sind auch sehr hilfreich.