標籤:perl 小駱駝 習題
1 請用perl在螢幕輸出hello,world
[[email protected] perl]# cat perlhello.pl #!/usr/bin/perlprint "hello,world!\n";[[email protected] perl]# ./perlhello.pl hello,world!
2 截取出正則的匹配內容,在shell中,真是頭都大了
#!/usr/bin/perl -w$_="<span class=\"title\">Counter-Strike Global Ofensive</span>";if(/<span class=\"title\">(.*)<\/span>/){print "$1\n";}
[[email protected] perl]# ./perlre.pl Counter-Strike Global Ofensive
稍微改動下,截取括弧的內容,也是很簡單
#!/usr/bin/perl -w
#注意每一句以分號結尾,注意是否轉義
$_="(Counter-Strike Global Ofensive)";if(/\((.*)\)/){print "$1\n";}
3 接受標準輸入,並輸出
#!/usr/bin/perl -wwhile (<>){print;}
[[email protected] perl]# ./receive.pl
hello,i am liuliancao
hello,i am liuliancao
4 計算圓的面積
#!/usr/bin/perl -wprint "please input the radius of the circle";$r=<STDIN>;if($r lt 0){$c=0;}else{$c=2*$r*3.141592654;}print "the c is $c\n";
[[email protected] perl]# ./circle.pl please input the radius of the circle:1 the c is 6.283185308
5 計算兩個數的乘積,並輸出它們
#!/usr/bin/perl -wprint "please input two number divided by Enter,and i will mutiply them\n";chomp($a=<STDIN>);$b=<STDIN>;print "a is $a, and b is $b and the muti result is ",$a*$b," !\n";
[[email protected] perl]# ./muti.pl
please input two number divided by Enter,and i will mutiply them
2
5
a is 2, and b is 5
and the muti result is 10 !
6 重複輸出指定次數字串
#!/usr/bin/perl -wprint "please input string and times divided by Enter!\n";chomp ($a=<STDIN>);$b=<STDIN>;$result=$a x $b;print "result is $result\n";
[[email protected] perl]# ./stringtimes.pl please input string and times divided by Enter!liuliancao5result is liuliancaoliuliancaoliuliancaoliuliancaoliuliancao
本文出自 “啟學的學習之路” 部落格,請務必保留此出處http://qixue.blog.51cto.com/7213178/1659264
perl小程式(一)