Perl Language(II)
4. Control Construct
4.1.2 true/false
#!/usr/bin/perl
print "input number=";
my $num = <STDIN>;
chomp($num); # remove the \n of this parameter
if($num < 5){
print "small";
}else{
print "big";
}
console output:
input number=18
big
4.1.4 parameter life time
#!/usr/bin/perl
my $num = 3;
{
my $max = $num;
print "inner value=".$max."\n";
}
print "outer value=".$max;
console output:
inner value=3
outer value=
compare number: <, >, >=, <=, ==, !=
compare string : eq, lt, gt, le, ge, ne
4.2.2 unless
#!/usr/bin/perl
my $a = 5;
unless($a<3){ #unless vs if
print "$a<3 is false\n";
}
$a = 1;
if($a<3){
print "$a<3 is true";
}
console output:
5<3 is false
1<3 is true
4.3.4 else/elsif
if ($num == 1) {
...
} else {
...
}
if ($date eq '星期一') {
....
} elsif ($date eq '星期二') {
....
} elsif ($date eq '星期三') {
....
} else {
print "你怎麼會有$date\n";
}
my $num = 1;
my $result = 1;
while ($num <= 10) {
$result*=$num;
$num = $num + 1;
}
4.3.2 until
my $num = 1;
my $result = 1;
until ($num > 10) {
$result*=$num;
$num = $num + 1;
}
console output:
num=2 result=1
num=3 result=2
num=4 result=6
4.4 for loop
my $result = 1;
for (my $num = 1; $num <= 10; $num = $num + 1) {
$result *= $num;
}
for my $num (1...10) { # $num from 1 to 10
print $num;
}
equal to
for (1...10) {
print $_;
}
equal to
print for (1...10);
4.4.4 element in array
#!/usr/bin/perl
my @array = qw/1 2 3 4 5/;
for (my $i = 0; $i <= $#array; $i++) {
print $array[$i];
}
print "\n";
print foreach (@array);
console output:
12345
12345
#!/usr/bin/perl
my @array = qw/6 -4 8 12 -22 19 -8 42/;
my @positive;
for (@array) {
push @positive, ($_**2) if ($_ > 0); #if element > 0, **2 and push to positive
}
print join "," , @positive;
console output:
36,64,144,361,1764
references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/
4. Control Construct
4.1.2 true/false
#!/usr/bin/perl
print "input number=";
my $num = <STDIN>;
chomp($num); # remove the \n of this parameter
if($num < 5){
print "small";
}else{
print "big";
}
console output:
input number=18
big
4.1.4 parameter life time
#!/usr/bin/perl
my $num = 3;
{
my $max = $num;
print "inner value=".$max."\n";
}
print "outer value=".$max;
console output:
inner value=3
outer value=
compare number: <, >, >=, <=, ==, !=
compare string : eq, lt, gt, le, ge, ne
4.2.2 unless
#!/usr/bin/perl
my $a = 5;
unless($a<3){ #unless vs if
print "$a<3 is false\n";
}
$a = 1;
if($a<3){
print "$a<3 is true";
}
console output:
5<3 is false
1<3 is true
4.3.4 else/elsif
if ($num == 1) {
...
} else {
...
}
if ($date eq '星期一') {
....
} elsif ($date eq '星期二') {
....
} elsif ($date eq '星期三') {
....
} else {
print "你怎麼會有$date\n";
}
my $num = 1;
my $result = 1;
while ($num <= 10) {
$result*=$num;
$num = $num + 1;
}
4.3.2 until
my $num = 1;
my $result = 1;
until ($num > 10) {
$result*=$num;
$num = $num + 1;
}
console output:
num=2 result=1
num=3 result=2
num=4 result=6
4.4 for loop
my $result = 1;
for (my $num = 1; $num <= 10; $num = $num + 1) {
$result *= $num;
}
for my $num (1...10) { # $num from 1 to 10
print $num;
}
equal to
for (1...10) {
print $_;
}
equal to
print for (1...10);
4.4.4 element in array
#!/usr/bin/perl
my @array = qw/1 2 3 4 5/;
for (my $i = 0; $i <= $#array; $i++) {
print $array[$i];
}
print "\n";
print foreach (@array);
console output:
12345
12345
#!/usr/bin/perl
my @array = qw/6 -4 8 12 -22 19 -8 42/;
my @positive;
for (@array) {
push @positive, ($_**2) if ($_ > 0); #if element > 0, **2 and push to positive
}
print join "," , @positive;
console output:
36,64,144,361,1764
references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/