2013年3月4日 星期一

Perl初學筆記

哼哼,剛好有同事教我的啦,加減學摟!
大部分的題目是練習自歐萊禮的Perl學習手冊裡面的練習題。

取得使用者輸入的值的練習,並且印出Welcome + 人名 :
#!/usr/bin/perl
#thisn is a comment
print "what\'s your name\?";
print "\n";
$line = <STDIN>;
if($line eq "\n"){
    print "nothing";
}else{
    print "Welcome  $line !";
}


執行結果:
[winwu@XXXXX (00:05:44) ~/my_perl]$ perl ch01.pl
what's your name?
win wu
Welcome  win wu


計算圓周長(半徑是12.5):
#!/usr/bin/perl -w
$radius = 12.5;
$pi = 3.141592654;
$circum = $radius*(2*$pi);
print "The circumference is  : $circum";

執行結果:
[winwu@XXXXX (00:13:23) ~/my_perl]$ perl ch02_1.pl
The circumference is  : 78.53981635



檔名 ch02_2.pl(練習第二章的第2跟第3題)
#!/usr/bin/perl -w
print "Please Enter the radius";
print "\n";
$your_radius = <stdin>;
if($your_radius lt 0){
$your_radius = 0;
}
#if the the user enter < 0, than we use 0
$pi = 3.1415926;
$result = $your_radius*(2*$pi);
print "\n"."The Circumference is $result\n";

執行結果:
[winwu@XXXXXXX (00:24:52) ~/my_perl]$ perl ch02_2.pl
Please Enter the radius
12.5
The Circumference is 78.539815



檔名ch02_4.pl(練習第二章的第4題)
讓使用者分別輸入兩個號碼,並且相乘

#!/usr/bin/perl -w
print "Please Enter the First Number\n";
chomp($first_num = <STDIN>);
print "\nPlease Enter the Second Number\n";
chomp($sec_num = <STDIN>);
$result = $first_num*$sec_num;
print "\nThe Result is $result";

執行結果:
[winwu@XXXXXXX (00:32:47) ~/my_perl]$ perl ch02_4.pl
Please Enter the First Number
10
Please Enter the Second Number
20
The Result is 200


檔名 ch02_5.pl(練習第二章的第5題)
#!/usr/bin/perl -w
print "\nPlease Enter an String\n";
$your_str = <STDIN>;
#chomp($your_str);
print "\nPlease Enter an Number\n";
chomp($your_num = <STDIN>);
$result = $your_str x $your_num;
#use X to replace *
print "\n$result";

執行結果:
[winwu@XXXXXX (00:49:12) ~/my_perl]$ perl ch02_5.pl
Please Enter an String
wggw
Please Enter an Number
3
wggw
wggw
wggw

  • 用#!/usr/bin/perl -w時,變數最好要用my宣告
  • windows下可安裝 Komodo IDE來練習Perl

--------------------這是華麗的分隔線---------------



(以下)的練習很多都是參考自簡信昌先生所寫的Perl學習手札而來的:
請參考以上網址,謝謝。

印出'\n' :
#!/usr/bin/perl -w
use strict;

print "\\n";

執行結果:
\n



取出陣列所有大於50的值 :
#!/usr/bin/perl -w
use strict;
my @array = qw/6 -4 8 10 -22  30 40 60 70 60 40 60/;
my @positive = grep{$_>40 } @array;
print "$_\n" for @positive;

執行結果:
[XXXXX@XXXXX (19:26:35) ~/my_perl]$ perl ch201301.pl
60
70
60
60


建立一個陣列(10 15 20 25 30 35),並讓使用者輸入索引值,並且印出陣列中相對應的值:  
#!/usr/bin/perl -w
use strict;
my @array = qw/10 15 20 25 30 35/;
print "Please enter which Key's value you want to see(enter 0~6)?\n";
my $your_value = <STDIN>;
chomp($your_value);
print "the \@array[$your_value] is : \n";
print $array[$your_value]."\n";

執行結果:
[XXXX@XXXXX(19:33:48) ~/my_perl]$ perl ch201301.pl
Please enter which Key's value you want to see(enter 0~6)?
5
the @array[5] is :
35

把這個陣列(4 -4 5 -6 7 -8 9 -10)做排序,並且印出結果 :  
#!/usr/bin/perl -w
use strict;
my @array = qw/4 -4 5 -6 7 -8 9 -10/;
@array = sort @array;
print join ' < ', @array;

執行結果:
[XXXX@XXX (19:41:34) ~/my_perl]$ perl ch201301.pl
-10 < -4 < -6 < -8 < 4 < 5 < 7 < 9



將陣列(4 -4 5 -6 7 -8 9 -10)中所有的值除以2然後print出來:

#!/usr/bin/perl -w
use strict;
my @array = qw/4 -4 5 -6 7 -8 9 -10/;
@array = map{$_/2} @array;
print join "\n", @array;

執行結果:
[XXXXX@XXXXX (19:44:24) ~/my_perl]$ perl ch201301.pl
2
-2
2.5
-3
3.5
-4
4.5


沒有留言:

張貼留言

若你看的文章,時間太久遠的問題就別問了,因為我應該也忘了... XD

Vue multiselect set autofocus and tinymce set autofocus

要在畫面一進來 focus multiselect 的方式: 參考: https://jsfiddle.net/shentao/mnphdt2g/ 主要就是在 multiselect 的 tag 加上 ref (例如: my_multiselect), 另外在 mounted...