Perl One衬垫适用于3种情况

0 perl conditional-operator

我有这个

if($x<10){                                  
    print "child";
}elseif($x>10 && $x<18){
    print "teenage"
}else{
    print "old"
}
Run Code Online (Sandbox Code Playgroud)

我想要穿上perl one liner我怎么能这样做请帮助我

Dan*_*Dan 5

您可以使用条件运算符.你也只需说print一次 - 我也会改变你的条件,因为10既不是也不>10<10,但你的代码认为10old.

print $x<10 ? 'child' : $x<18 ? 'teenage' : 'old';
Run Code Online (Sandbox Code Playgroud)