實數的 for 迴圈可能不會達到您的期望;像 0.1 這樣的數字是重複的二進制分數,當截斷為 64 位雙精度數字時,這些數字要么稍微太小,要么稍微太大。 在這個例子中,非常接近3.0的最終值實際上有點大。
這
儘管迴圈比 for 迴圈更簡單,但可能不太常見。 這個例子會更容易閱讀
為了環形;這
下一個例子是更好的使用
儘管。
authors = {"Margaret Mitchell", "J.D. Salinger", "Emily Brontë"};
iAuthor = 1;
While( iAuthor <= N Items( authors ),
Write( "\!n", authors[iAuthor], " wrote one novel" );
iAuthor += 1;
);
瑪格麗特·米切爾寫了一本小說JD 塞林格寫過一本小說艾米莉·勃朗特寫了一本小說
這儘管循環可以透過刪除元素直到列表為空來處理列表。
animals = {"octopus", "spider", "scorpion"};
While( N Items( animals ) > 0,
// RemoveFrom modifies the animals list
// and returns a list of (in this example)
// one item
animal = Remove From( animals, 1 );
// subscript the list to get just the animal
// name, without the curly braces for the list
Write( "\!n", animal[1], " has eight legs" );
);章魚有八條腿
蜘蛛有八條腿
蝎子有八條腿
這
公式欄循環在 JMP 內部完成。 當您在資料表中建立公式列時,JMP 會計算每行的公式。 稍後,當您變更公式所依賴的另一列中的值時,JMP 將重新評估依賴於變更後的值的行。 JMP 在空閒時間進行這些評估。 如果您希望 JMP 更快更快地完成這些操作,請向資料表發送 <
dt = Open( "$sample_data/big class.jmp" );
hw = dt << New Column( "HW_Ratio", formula( height / weight ) );
dt << RunFormulas; // all 40 values are computed
dt << Sort(by (HW_Ratio), replaceTable); // tall enough for my weight?
Table with formula columnTable with formula columnTable with formula columnTable with formula column帶有公式列的表格
許多函數需要一個矩陣參數並將函數應用於每個元素。 傳回結果矩陣。
s = Round(Sqrt( [1, 2, 3, 4, 5, 6, 7, 8, 9] ), 2);
write(s); // [1, 1.41, 1.73, 2, 2.24, 2.45, 2.65, 2.83, 3]
矩陣函數
洛克循環遍歷矩陣並傳回 Loc 標識的元素的位置。 這比 for 迴圈快得多。
s = Round(Sqrt( [1, 2, 3, 4, 5, 6, 7, 8, 9] ), 2);
write(Loc(s > 2)); // [5, 6, 7, 8, 9] (element 4 is equal)
使用
矩陣下標可以比 for 迴圈更快重組矩陣。
s = Round( Sqrt( [1, 2, 3, 4, 5, 6, 7, 8, 9] ), 2 );
s[Loc( s > 2 )] = -1;
Write( s ); // [1, 1.41, 1.73, 2, -1, -1, -1, -1, -1]
超出這一點的一切都是例外除非你有充分的理由,否則應該避免使用這種使用模式。 以後會很難理解,其他人也很難維護。
這
J函數建立一個矩陣並用一個值對其進行初始化。 如果該值是一個表達式,則會對每個元素進行計算。 在此範例中,J() 的第三個參數是括號中的一對語句;第一個語句遞增計數器,第二個語句計算一個值。 最後計算的值是由多個語句組成的表達式的值。
idx = 0; // not a recommended pattern!
mat = J( 2, 3, (idx += 1 ; Sqrt( idx )) );
Write( Round( mat, 1 ) ); // [1 1.4 1.7, 2 2.2 2.4]
這
平行分配函數使用多個線程來填充數組。 與 J 函數的第三個參數不同,您不能依賴正在計算的元素的任何特定順序。 並行分配嘗試使執行緒彼此隔離,並且確實不希望其表達式存取全域資料。
bigMat = J( 500, 500, . );
// these are global x and y values that are only copied into each thread, below.
x = 2;
yy = 3;
// x=x and y=yy is copying global values to local values of the same/similar name.
// bigMat[rr, cc] determines the array that is being filled in and the
// row and column names you'll use in the expression after the =.
// the if statement is an example expression, not terribly interesting but
// typical of the kind of thing parallel assign does well.
rc = Parallel Assign( {x = x, y = yy}, bigMat[rr, cc] = If( x == 2, rr * cc, y ) );
// Max dose NOT return an array, it returns the maximum value in the array.
// dividing by max scales the values from 0 to 1
bigMat = bigMat / Max( bigMat );
// new image expects values from 0 to 1
New Window( "demo", New Image( "rgb", {bigMat, 1 - bigMat, bigMat} ) );
250,000 if statements evaluated250,000 if statements evaluated250,000 if statements evaluated250,000 if statements evaluated評估了 250,000 個 if 語句這
模式匹配器可以循環字串並在比賽進行時運行 JSL。
text = // use the \[ and ]\ to escape the quotes in this text
"\[John Bartlett, who ran the University Book Store in Cambridge,
Massachusetts, was frequently asked for information on quotations and
he began a commonplace book of them for reference. In 1855, he
privately printed his compilation as A Collection of Familiar Quotations.
This first edition contained 258 pages of quotations by 169 authors,
chiefly the Bible, William Shakespeare, and the great English poets.
Bartlett wrote in the fourth edition that "it is not easy to determine in
all cases the degree of familiarity that may belong to phrases and
sentences which present themselves for admission; for what is familiar
to one class of readers may be quite new to another." - Wikipedia]\";
words = [=> 0]; // an associative array that defaults unknown keys to a value of 0
// this match repeats a pair of alternatives: a run of a-z or a run of NOT a-z.
// the runs of a-z are stored in an associative array
rc = Pat Match(
text,
Pat Repeat( // the loop starts here
(Pat Regex( "[a-zA-Z]+" ) >> word
+Pat Test( // insert word into words assoc array
words[Lowercase( word )] += 1; // this JSL is evaluated for each word
1; // the test must succeed for the pattern to continue
)) | Pat Regex( "[^a-zA-Z]+" ) // skip non-words
)
);
// make a table, load it from the words assoc array keys and values
dt = New Table( "word count",
New Column( "word", character, Set Values( words << getkeys ) ),
New Column( "count", Set Values( words << getvalues ) )
);
// sort by the count column
dt << sort( by( count ), order( descending ), replaceTable );
Table of word countsTable of word countsTable of word countsTable of word counts字數統計表
這
調度程式可以每隔幾秒鐘一次又一次地運行您的程式碼。
n = 0; // counter, just for this demo
dowork = Function( {},
Print( "hi", n );
n += 1;
If( n < 10,
Schedule( .5, dowork() );// half second from now, run me again
, // else
Schedule( 0, 0 ) << close; // no delay, no code
);
);
dowork(); // start the loop
遞迴可以穿過一棵樹。
// this tree walker looks for button boxes in a display
// box tree and prints the button name
treeWalker = Function( {box}, // parameter
{child = box << child}, // local variables
While( !Is Empty( child ),
If( child << classname == "ButtonBox",
Show( child << Get Button Name )
);
treeWalker( child ); // you could also use recurse(child)
child = child << sib;
)
);
x = V List Box(
H List Box( Button Box( "x" ), Button Box( "z" ) ),
Border Box( Button Box( "w" ) )
);
treeWalker( x ); // shows x, z, and w