您的位置:首页 > 其它

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";
############################
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Perl Learning