解
使用for(...)或while(...)或其中一个更奇特的变体。
该
对于循环是重复某些语句的最常用方法。
Cars = {"Ford", "Chevy", "Tesla"}; // a list
nCars = N Items( Cars );
// notice loop starts at one and <= counts to the end
For( iCar = 1, iCar <= nCars, iCar += 1,
car = Cars[iCar]; // JSL lists are 1-based
Write( "\!n", car, " has four wheels." );
);
福特有四个轮子。
雪佛兰有四个轮子。
特斯拉有四个轮子。
具有实数的for循环可能无法达到您的预期;像0.1这样的数字是重复的二进制分数,当被截断为64位双精度数时,它们要么稍微过小,要么稍微过大。在这个例子中,非常接近3.0的最终值实际上有点大。
for(i=2, i<= 3, i+=.1, print(i);); // 2, 2.1, ... 2.9 (but not 3.0)
write("\!nFinally ",i-3); // 8.88178419700125e-16
2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 最后8.88178419700125e-16
如果您知道需要整数个步骤,请对for语句使用整数并从中计算浮点值。
for(i=0,i<=10,i++,
write(" ", 2 + i/10);
// 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3
);
2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3
该
而循环比for循环更简单,但可能不太常见。这个例子用a更容易阅读
对于环;该
下一个例子是更好的使用
而 。
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塞林格写了一部小说艾米莉勃朗特写了一部小说
该而 loop可以通过删除元素来处理列表,直到列表为空。
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" );
);
章鱼有八条腿
蜘蛛有八条腿
蝎子有八条腿
对于和
而循环可以使用
打破 ()语句提前打破循环和
继续 ()语句跳过剩余的语句并继续在顶部。Continue()最适合使用顶部增量的for循环。
For( i = 1, i <= 10, i++,
If( i == 3, Continue() );
If( i == 7, Break() );
Write( " ", i ); // 1 2 4 5 6
);
1 2 4 5 6
ForEachRow 使用数据表,是一种可以通过其他方式完成的快捷方式。ForEachRow的主要问题是它适用于当前数据表。因此,如果有多个表打开,我建议不要使用它,我绝对不建议尝试嵌套ForEachRow循环,因为内部循环和外部循环会相互混淆当前行。
dt = Open( "$sample_data/big class.jmp" );
For Each Row(
If( Starts With( dt:name, "A" ),
Write( " ", dt:name ); // ALICE ALFRED AMY
)
);
爱丽丝阿尔弗雷德
该配方栏循环在JMP内部完成。在数据表中创建公式列时,JMP会评估每行的公式。稍后,当您更改公式所依赖的另一列中的值时,JMP将重新评估依赖于更改值的行。JMP在空闲时间进行这些评估。如果您希望JMP更快更快地执行它们,请向数据表发送<< RunFormulas消息。
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 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)
运用
矩阵下标可以比循环更快地重组矩阵。
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()的第三个参数是parens中的一对语句;第一个语句递增计数器,第二个语句计算一个值。计算的最后一个值是由多个语句组成的表达式的值。
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函数3rd参数不同,您不能依赖于计算元素的任何特定顺序。并行分配尝试保持线程彼此隔离,并且实际上不希望其表达式访问全局数据。
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 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 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
你可以用 for 循环来实现这一点。根据问题的不同,你可能会发现数组下标更快、更容易。数组或数据表可以用三元组来下标,比如
x[ start::stop::step ]
返回 x 的切片。或者你可以使用
for( i = start, i <= stop, i+= step,
...statements separated by semicolons...
)
if you want to move the window, put an outer loop around it to adjust the start and stop values.