Perl Learning - 2 (If(), While(), Chomp(), <STDIN>)
2012-11-02 18:01
323 查看
Like other programing languages, Perl has if() and while() to perform judgement and loop.
if(condition){
do something if condition is true;
}else{
do something else if condition is false;
}
condition can be Perl atatement or a value, else is not a must being being behind a if(). Examples:
if($name gt 'fred'){
print "'$name' comes after 'fred' in sorted order.\n";
}
#############
$is_bigger=$name gt 'fred';
if($is_bigger){...}
If value is num 0 or character '0' or undef, then it means false, otherwise it's true.
Users can input test in Perl by <STDIN>, it's standard input device(typically keyboard).
$line=<STDIN>;
if($line eq "\n"){
print "That was just a blank line.\n";
}else{
print "That line input was: $line";
}
Each line from <STDIN> ends by \n (enter), to ends typing from keyboard please type "Ctrl + d".
If you want a line exclude \n, use chomp. Chomp removes the last ONE "\n" and returns the number of removed character(\n), the returned value (always 1) is seldom used.
$line=<STDIN>;
chomp($line);
Another way most Perlers are using:
chomp($line=<STDIN>);
Forgot to mention while() for loop ...
While(condition){
keep doing something while condition is true;
do something to condition to make it false, then the loop ends itself;
}
The condition is exactly the same as if() uses.
$a=1;
while($a<10){
$sum+=$a;
$a+=2;
}
It caculates the sum of all single numbers smaller than 10. while() whill judge the contition fist, if the first judgement is false then loop do nothing else.
Execsices and my programs:
1. Write a perl program, to caculate the circumference of a circle, r=12.5; c=2pai * r, pai=3.14.
#!/usr/bin/perl
use strict;
use warnings;
my $pai=3.141592654;
my $r=12.5;
my $cir=$r * 2 * $pai;
print "The circle is $cir\n";
############################
2. Modify #1 program, to let use input his r.
#!/usr/bin/perl
use strict;
use warnings;
my $pai=3.141592654;
print "Your r is: ";
chomp(my $r=<STDIN>);
my $cir=$r * 2 * $pai;
print "The circle is $cir\n";
############################
3. Modify #2 program, when use input r less than 0, it outputs 0 but not less than 0.
#!/usr/bin/perl
use strict;
use warnings;
my $pai;
my $r;
my $cir;
$pai=3.141592654;
print "Your r is: ";
chomp($r=<STDIN>);
if($r<0){$cir=0;}
else{$cir=$r * 2 * $pai;}
print "The circle is $cir\n";
############################
4. Write a program, let use input 2 numbers, each number one line. Output their product.
#!/usr/bin/perl
use strict;
use warnings;
my $num1;
my $num2;
my $sum;
chomp($num1=<STDIN>);
chomp($num2=<STDIN>);
$sum=$num1*$num2;
print "$sum\n";
############################
5. Modify #4 to let user input more lines containing only numbers, each line one number. Until user ends inputing, output their product.
#!/usr/bin/perl
my $num;
my $sum=1;
while(chomp($num=<STDIN>)){
if($num=~/\d+/){
$sum*=$num;
}
else{
print "$sum\n";
exit;
}
}
print "$sum\n";
############################
6. Write a program, let user input a line of characters and a number in two lines. Output lines of the characters, line number is the number user input.
#!/usr/bin/perl
my $char;
my $num;
$char=<STDIN>;
chomp($num=<STDIN>);
$text="$char" x $num;
print "$text";
############################
if(condition){
do something if condition is true;
}else{
do something else if condition is false;
}
condition can be Perl atatement or a value, else is not a must being being behind a if(). Examples:
if($name gt 'fred'){
print "'$name' comes after 'fred' in sorted order.\n";
}
#############
$is_bigger=$name gt 'fred';
if($is_bigger){...}
If value is num 0 or character '0' or undef, then it means false, otherwise it's true.
Users can input test in Perl by <STDIN>, it's standard input device(typically keyboard).
$line=<STDIN>;
if($line eq "\n"){
print "That was just a blank line.\n";
}else{
print "That line input was: $line";
}
Each line from <STDIN> ends by \n (enter), to ends typing from keyboard please type "Ctrl + d".
If you want a line exclude \n, use chomp. Chomp removes the last ONE "\n" and returns the number of removed character(\n), the returned value (always 1) is seldom used.
$line=<STDIN>;
chomp($line);
Another way most Perlers are using:
chomp($line=<STDIN>);
Forgot to mention while() for loop ...
While(condition){
keep doing something while condition is true;
do something to condition to make it false, then the loop ends itself;
}
The condition is exactly the same as if() uses.
$a=1;
while($a<10){
$sum+=$a;
$a+=2;
}
It caculates the sum of all single numbers smaller than 10. while() whill judge the contition fist, if the first judgement is false then loop do nothing else.
Execsices and my programs:
1. Write a perl program, to caculate the circumference of a circle, r=12.5; c=2pai * r, pai=3.14.
#!/usr/bin/perl
use strict;
use warnings;
my $pai=3.141592654;
my $r=12.5;
my $cir=$r * 2 * $pai;
print "The circle is $cir\n";
############################
2. Modify #1 program, to let use input his r.
#!/usr/bin/perl
use strict;
use warnings;
my $pai=3.141592654;
print "Your r is: ";
chomp(my $r=<STDIN>);
my $cir=$r * 2 * $pai;
print "The circle is $cir\n";
############################
3. Modify #2 program, when use input r less than 0, it outputs 0 but not less than 0.
#!/usr/bin/perl
use strict;
use warnings;
my $pai;
my $r;
my $cir;
$pai=3.141592654;
print "Your r is: ";
chomp($r=<STDIN>);
if($r<0){$cir=0;}
else{$cir=$r * 2 * $pai;}
print "The circle is $cir\n";
############################
4. Write a program, let use input 2 numbers, each number one line. Output their product.
#!/usr/bin/perl
use strict;
use warnings;
my $num1;
my $num2;
my $sum;
chomp($num1=<STDIN>);
chomp($num2=<STDIN>);
$sum=$num1*$num2;
print "$sum\n";
############################
5. Modify #4 to let user input more lines containing only numbers, each line one number. Until user ends inputing, output their product.
#!/usr/bin/perl
my $num;
my $sum=1;
while(chomp($num=<STDIN>)){
if($num=~/\d+/){
$sum*=$num;
}
else{
print "$sum\n";
exit;
}
}
print "$sum\n";
############################
6. Write a program, let user input a line of characters and a number in two lines. Output lines of the characters, line number is the number user input.
#!/usr/bin/perl
my $char;
my $num;
$char=<STDIN>;
chomp($num=<STDIN>);
$text="$char" x $num;
print "$text";
############################
相关文章推荐
- Perl Learning - 5 (sort(), reverse(), context, list <STDIN>)
- perl 里的<>和<STDIN>
- Perl Learning - 14 (Catch var, (:?), (:<LABLE>), \g{LABLE}, {m,n}
- 一道腾讯的笔试题,不使用任何判断语句求出两个数中的较大者(即 if、if...else...、>、<、==、while等语句)
- perl读取文件并处理(while<>)
- Perl Learning - 8 (I/O, <>, print(), @ARGV)
- WF4B1 的Procedural Activity 之Assign,WriteLine,IF,While,DoWhile,Switch<T>使用
- perl 里的<>和<STDIN>
- 条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
- mybatis在<if>中传入一个简单类型参数的两种写法
- mybatis中<set>、<where>、<if>的使用
- 面试题:求整数a,b的最大值,不许使用if,switch,<,>,?,:
- HTML中IE条件注释判断语句(<!--[if XX IE X]><![endif]-->)
- 条件注释判断浏览器版本<!--[if lt IE 9]>
- jsp小知识02-------<c:if>
- <!--[if IE]>….<![endif]-->&…
- JSP的<c:choose><c:forEach><c:if><c:when><c:set>标签
- <!–[if lte IE 6]>……<![endif]–>
- 运行Spark项目出错error: error while loading <root>, error in opening zip file
- perl&nbsp;sort&nbsp;&nbsp;&nbsp;&lt;=&gt;&nbsp;and&nbsp;cmp