基于在最新版本的Gnuplot中对command.c
的源代码的简要检查,我会说这个功能不被支持。更具体地说,相关部分可以在1163
(见下面)中找到。解析器首先确保if
后面跟着括号中的条件。如果以下标记为{
,则它会激活新语法,将封闭在匹配的一对{}
中的整个if块隔离,并且可选地查找else
,但是只允许使用{}
(含)条款。由于这个原因,一个简单的脚本,例如:
if(flag == 1){
print 1;
}else if(flag == 2){
print 2;
}
确实产生错误信息expected {else-clause}
。一个解决办法是嵌套if语句为:
if(flag == 1){
}else{
if(flag == 2){
}else{
if(flag == 3){
}
}
}
这是无可否认稍微详细...
void
if_command()
{
double exprval;
int end_token;
if (!equals(++c_token, "(")) /* no expression */
int_error(c_token, "expecting (expression)");
exprval = real_expression();
/*
* EAM May 2011
* New if {...} else {...} syntax can span multiple lines.
* Isolate the active clause and execute it recursively.
*/
if (equals(c_token,"{")) {
/* Identify start and end position of the clause substring */
char *clause = NULL;
int if_start, if_end, else_start=0, else_end=0;
int clause_start, clause_end;
c_token = find_clause(&if_start, &if_end);
if (equals(c_token,"else")) {
if (!equals(++c_token,"{"))
int_error(c_token,"expected {else-clause}");
c_token = find_clause(&else_start, &else_end);
}
end_token = c_token;
if (exprval != 0) {
clause_start = if_start;
clause_end = if_end;
if_condition = TRUE;
} else {
clause_start = else_start;
clause_end = else_end;
if_condition = FALSE;
}
if_open_for_else = (else_start) ? FALSE : TRUE;
if (if_condition || else_start != 0) {
clause = new_clause(clause_start, clause_end);
begin_clause();
do_string_and_free(clause);
end_clause();
}
你能避免使用在你的第二个例子'else'并获得你所需要的。 –