用Euler项目教自己Perl.Anywho,
print "Hei" if 1==1;
Run Code Online (Sandbox Code Playgroud)
奇迹般有效.
是否可以在if之前包含几个语句,如此
{print "4";print="2";} if 4!=2;
Run Code Online (Sandbox Code Playgroud)
我知道具体的语法不起作用,但我认为我想要做的是显而易见的.可能与否?
PS.我也知道我可以用常规做到这一点
if(){}
Run Code Online (Sandbox Code Playgroud)
1)如前所述,您可以使用do块:
do {print "4";print "2";} if 4!=2;
Run Code Online (Sandbox Code Playgroud)
2)你可以像在C中一样使用逗号:
print("4"), print("2") if 4!=2;
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,您必须用括号写.
3)我们知道print()返回"1",所以:
print("4") && print("2") if 4!=2;
Run Code Online (Sandbox Code Playgroud)
当第一个命令返回true时,这将起作用.
4)使用二元运算符:
print("4") | print("2") if 4!=2;
print("4") & print("2") if 4!=2;
print("4") ^ print("2") if 4!=2;
# etc
Run Code Online (Sandbox Code Playgroud)
我认为这应该始终有效但不常见.
5)使用"阵列"
(print("4"), print("2")) if 4!=2;
Run Code Online (Sandbox Code Playgroud)
6)连接
print("4") . print("2") if 4!=2;
Run Code Online (Sandbox Code Playgroud)
7)man perlop并找到更多;)
*)写一个好人:
if( 4!=2 ) {
print("4");
print("2");
}
Run Code Online (Sandbox Code Playgroud)
只需do在它之前添加一个:
do {print "4";print="2";} if 4!=2;
Run Code Online (Sandbox Code Playgroud)
请注意,我不会在任何现实场景中推荐这种代码.正常
if (condition)
{
code;
}
Run Code Online (Sandbox Code Playgroud)
表单更加熟悉,更易于阅读和调试,并且有缩进指导读者关于控制流程.