循環中全域變數被覆蓋你好,
我使用循環逐列建立視窗。例如,在下面的 JSL 範例中,我為每一列(年齡、體重)建立一個顯示分佈的視窗和一個用於臨時隱藏視窗的按鈕。問題是,隨著迴圈的進行,我的物件變數會被上一個視窗覆蓋。
例如,在範例產生的年齡窗口中,按一下按鈕本應暫時隱藏年齡窗口,但實際上卻隱藏了體重窗口,因為它已被覆蓋。
能否設定一個預設名稱(例如“此處”),這樣每個視窗都有自己獨立的空間,而無需每次都為每個視窗更改變數名稱?因為我有很多視窗…
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
lstCols = {"age", "weight"};
For Each( {value, index}, lstCols,
colref = Column( value );
window = New Window( "Window - " || value,
V List Box(
distribution = dt << Distribution( Column( colref ) ),
Button Box( "Hide",
window << show window( 0 );
Wait( 2 );
window << show window( 1 );
)
);
);
);
自動化和腳本編寫回覆:循環中全域變數被覆蓋您可以使用視窗範圍
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};
nws = {};
For Each({value, index}, lstCols,
colref = Column(value);
nw = New Window("Window - " || value,
V List Box(
window:dist = dt << Distribution(Column(colref)),
Button Box("Hide",
window:dist << show window(0);
Wait(2);
window:dist << show window(1);
)
);
);
Insert Into(nws, nw);
);
For Each({nw}, nws,
Show(nw:dist << Get Window Title)
);
您也可以評估對按鈕腳本/函數的引用,或在按鈕中使用稍微複雜的函數來考慮作用域。
回覆:循環中全域變數被覆蓋下面這個解決方案雖然不如@jthi的答案那麼優雅,但從我的角度來看更直觀。
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
lstCols = {"age", "weight"};
windows = Associative Array();
For Each( {value, index}, lstCols,
colref = Column( value );
Eval(EvalExpr(
windows[value] = New Window( "Window - " || value,
V List Box(
distribution = dt << Distribution( Column( colref ) ),
Button Box( "Hide",
windows[Expr(value)] << show window( 0 );
Wait( 2 );
windows[Expr(value)] << show window( 1 );
)
);
);
));
);
關聯數組“windows”提供了一個容器,用於保存對不同視窗的引用,“Eval(EvalExpr(...))”用於確保特定按鈕的腳本不是在評估當前的“值”,而是在創建時的作用域中的值。
回覆:循環中全域變數被覆蓋+1. 視窗作用域的詳細說明請參閱: https://www.jmp.com/support/help/en/19.1/?os =mac&source=application#page/jmp/scoped-names.shtml
回覆:循環中全域變數被覆蓋您好,謝謝!問題解決了。
回覆:循環中全域變數被覆蓋您好,謝謝您的解答,這真是另一種方法!
回覆:循環中全域變數被覆蓋您也可以使用“<< 設定功能”,並將按鈕引用為“起始”點。
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};
For Each({colname}, lstCols,
nw = New Window("Window - " || colname,
V List Box(
dt << Distribution(Column(Eval(colname))),
Button Box("Hide", << Set Function(Function({this},
dist_ob = this << prev sib;
show(dist_ob[OutlineBox(2)] << get title);
)));
);
);
);
Write();
and in this case for hiding the window you could just use this << Show Window(0);</CODE></EM></P>
<P>有時也可以使用上下文框:</P>
<PRE><CODE class=" language-jsl">Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
lstCols = {"age", "weight"};
For Each({colname}, lstCols,
nw = New Window("Window - " || colname,
V List Box(
Context Box(
box:dist = dt << Distribution(Column(Eval(colname))),
Button Box("Hide",
show(Report(box:dist)[OutlineBox(2)] << get title)
);
)
);
);
);
Write();
我會根據具體情況使用所有這些方法(以及表達式求值)。