Perl 程式備忘錄


1。=~ # 意思是用來比對相符

2。!~ #意思是用來比對不符

3。print '第一個字串' . "跟這個字串中間多個點,意思是用來將這兩個字串結合起來 ";

4。print "OK " x 4 ; # 會連續列出四行 OK

5。print 1 + 2 . " "; # 顯示出 3 這個值

6。$input=<>; # <> 是標準輸入取值

7。chomp $iput; # 內建的函式,截去變數尾端的 換行字元

8。if (!$input) { print "你輸入的值是空的 "; }

9。$total=undef; # 將變數 $total 變回未定義狀態

10。$total += $i; # 同等於 $total = $total + $1;

11。chomp($input=<STDIN>); # 結合 6。跟 7。

12。if ($a<100) || ($b<100) {print wow;} # 如果 $a 或者 $b 小於 100,顯示 wow

13。$str = substr ABCD12345, 0, 5; # 使用 substr 函式取出 ABCD1 五個字元
       $str = substr ABCD12345, 5; # 取出 2345
       $str = substr ABCD12345, -1; # 取出最後一個字元 5
       $str = substr ABCD12345, -3, 2; # 最後倒數第三個開始兩個字元個字元,所以是取出 34

14。print length(123456); # 使用 length() 得知有幾個字元

15。print index(123456,3)," "; # 尋找 3 這個字串在 123456 的那個位置,顯示出 2,所以是 0 1 2 囉 ???????

16。print rand 100; # 傳回 0 到 100 之間的隨機亂數

17。print int(rand 100); # 傳回 0 到 100 之間的隨機整數

18。($sec,$min,$hour,$day,$mon,$year)=localtime(time);

       print "$sec ";
       print "$min ";
       print "$hour ";
       print "$day ";
       print $mon+1," ";
       print $year+1900," ";   # 列出時間,年要多加1900年,月要多加一個月,這樣才是正確的

19。qw(a b c d) # 同於 ("a","b","c","d")

20。(1,2,3,4) # 同於 (1..4)

21。@list=qw(1 2 3 4);

       print @list; # 列出 1234

22。print join ',' ,@list; # 接續21。,使用 join 將字元用逗號分開,可以列出 1,2,3,4

23。print split(/,/,$a); # split 可以將1,2,3,4 變成 1234

24。@total=(1,2,3,4,5,6,7);

       for ($i=0; $i<=$#total; $i++) {
           $j=$i+1;
           print "第 $j 陣列的值: $total[$i] ";
       }
       # 由 $#total 可以得知 6
       # $total[0]=1
       # $total[1]=2
       # $total[2]=3
       # $total[3]=4
       # $total[4]=5
       # $total[5]=6
       # $total[6]=7

25。24。的另一個寫法

@total=(1,2,3,4,5,6,7);
$i=0;
foreach $item (@total) {
        print "total[$i] 等於 $item ";
        $i++;
}

25。從陣列中取出最後一個字元

@total=(2, 4, 6, 1, 3, 5, "a", "b", "c");

$flast=pop @total;

print "$flast "; # 得到 c

print @total;

# 此時 @total 只剩下 (2, 4, 6, 1, 3, 5, "a", "b");

26。從陣列中最出第一個字元

@total=(2, 4, 6, 1, 3, 5, "a", "b", "c");

$first=shift @total; # $first 應為 2

print "$first ";

print @total;
# 此時 @total 應為 (4, 6, 1, 3, 5, "a", "b", "c");

27。將 $f2 變數的值塞到 @total 陣列最後面

@total=(2, 4, 6, 1, 3, 5, "a", "b", "c");

$f2="d";

push @total, $f2;

print @total;
# 此時 @total 應為 (2, 4, 6, 1, 3, 5, "a", "b", "c", "d");

28。承27。,陣列加入陣列

@total=(2, 4, 6, 1, 3, 5, "a", "b", "c");

@t2=qw( John Marry Kenny );

push @total, @t2;

print @total;
# 此時 @total 應為 (2, 4, 6, 1, 3, 5, "a", "b", "c", "John", "Marry", "Kenny");

29。unshift 將字串加入陣列前面

@total=(2, 4, 6, 1, 3, 5, "a", "b", "c");

$f1="A";

unshift @total, $f1;

print @total;
# 此時 @total 應為 ("A", 2, 4, 6, 1, 3, 5, "a", "b", "c");

30。列出檔案

@a = `ls -l /root`;

foreach $line (@a) {
        $line =~ /s+(S+)$/;
        $file = $1;
        print $file." ";
}

31。reverse 將陣列反向排列

32。sort 以 ASCII 順序來將陣列排序

@a =qw(11 1 5 7 2);

@b = sort @a;
print join ',',@b," ";

32。sort 以數值大小排序

@a =qw(11 1 5 7 2);

@b = sort {$a<=>$b} @a;
print join ',',@b," ";

33。雜湊

%list=(
        "A" => "a",
        "B" => "b",
        "C" => "c"
);
# 以上同等 %list=("A","a","B","b","C","c");

print $list{A}," ";
print $list{B}," ";
print $list{C}," ";

評論: 0 | 引用: 0 | 閱讀: 1959 | 列印 | 文件 | 轉發

發表評論
暱 稱: 密 碼:
網 址: E - mail:
驗證碼: 驗證碼圖片 選 項:
頭 像:
內 容:
  • 粗體
  • 斜體
  • 底線
  • 插入圖片
  • 超連結
  • 電子郵件
  • 插入引用
  • 表情符號