加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Learning - 18 (unless, until, elsif, for)

发布时间:2020-12-15 21:01:38 所属栏目:大数据 来源:网络整理
导读:unless expression: when condition is false execute the block. ? unless($fred=~/^[A-Z_]w*$/i){ ?print "The value of $fred doesn't look like a perl identifier name.n"; } ? It's the same as written like this: ? if($fred=~/^[A-Z_]w*$/i){ ?
unless expression: when condition is false execute the block.
?
unless($fred=~/^[A-Z_]w*$/i){
?print "The value of $fred doesn't look like a perl identifier name.n";
}
?
It's the same as written like this:
?
if($fred=~/^[A-Z_]w*$/i){
?# do nothing
}else{
?print "The value of $fred doesn't look like a perl identifier name.n";
}
?
Or:
?
if(!($fred=~/^[A-Z_]w*$/i)){
?print "The value of $fred doesn't look like a perl identifier name.n";
}
?
until expression: when condition is false execute the loop.
?
until($j>$i){
?$j *= 2;
}
?
if expression can be written without a block of {}
?
print "$n is a negative number.n" if $n < 0;
?
Perl execute 'if $n < 0' first,if it's true then execute the print expression,if false do nothing.
?
Those styles are all popular in Perl:
?
&error("Invalid input") unless &valid($input);
$i *=2 until $i > $j;
print " ",($n += 2) while $n < 10;
&greet($_) foreach @person;
?
If there are more than one expression,better to write them in {} blocks.
?
elsif expression:
?
if(condition){
?do something;
}elsif(condition){
?do something;
}elsif(condition){
?do something;
}else{
?do something;
}
?
my $bedrock=42;
$bedrock++;
?
my @people=qw{ Fred barney Fred wilma dino barney Fred pebbles };
my %count;
$count{$_}++ foreach @people;
?
++ means +1 byself,undef++ gets 1
?
my $n=++$m;
$m add 1,then give value to $n.
?
my $n=$m++;
give value to $n,then $m add 1.
?
for (init;test;increment){
?body;
?body;
}
?
for(i=1;$i<=10;$i++){
?print "I can count to $i!n";
}
?
Any of init,test,increment can be empty,but the symbol ';' should be there.
?
for($_="bedrock";s/(.)//;){ # whenever s/// successes,loop continue
?print "One character is: $1n";
}
?
for(1..10){
?print "I can count to $_!n":
}
?
Here 'for' is the same as foreach. for and foreach are the same in Perl,Perl will judge by the contents in (). If there're ';' in () then it's for,otherwise it's foreach.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读