Perl Learning - 2 (If(), While(), Chomp(), <STDIN&
发布时间:2020-12-15 21:03:07 所属栏目:大数据 来源:网络整理
导读: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
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 $cirn";
############################
?
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 $cirn";
############################
?
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 $cirn";
############################
?
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 "$sumn";
############################
?
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 "$sumn"; ??????? exit; ??????? } ??? }
print "$sumn";
############################
?
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";############################
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |