您好,登錄后才能下訂單哦!
#!/usr/bin/perl -w
#########################################################################
# File Name: test.pl
#########################################################################
print "hello word \n";
print "---- 變量定義用\$ 符號------\n";
$a=`echo xxxx`;
print "$a";
$b='aaaa'.'hhhh';
$c="bbbb";
$d="$b"."$c";
$d.='eeee';
print "$d\n";
print "d: $d\n";
$bl1 = 1>=0;
$bl2 = 1>=3;
print $bl1;
print $bl2;
print "---- if elsif else 條件判斷語句 ----\n";
$if1=2;
if($if1 == 1){
print "pass\n";
}elsif($if1 == 2){
print "==2\n";
}else{
print "fail\n";
}
#chomp($in1 = <STDIN>);
#print "your input: $in1\n";
#if(defined($in2)){
# print "1";
#}else{
# print "2";
#}
print " ------- \@ 符號來定義數組 ------ \n";
@array = (1,2,3,"4")x4;
print "$array[1]\n";
print "$#array\n";
print "$array[-1] \n";
$xtest="123"x5;
print "$xtest\n";
print "pop push 針對數組的末尾操作 \n";
print "pop 取出最后一個,返回數組剩下的, push將一個元素添加到末尾 \n";
print "shift unshift 為 pop push的反向操作,針對數組頭\n";
@array2 = 5..10;
print "$#array2 ---- $array2[-1] \n";
pop @array2;
print "$#array2 ---- $array2[-1] \n";
pop(@array2);
print "$#array2 ---- $array2[-1] \n";
print "---------- array3 ----------\n";
@array3 = 1..3;
print "$#array3 ---- $array3[-1] \n";
push @array3,9;
print "$#array3 ---- $array3[-1] \n";
push @array3,10...12;
print "$#array3 ---- $array3[-1] \n";
print "---------- array4 ----------\n";
@array4 = 1..3;
print "@array4 ---- $array4[0] \n";
shift @array4;
print "@array4 ---- $array4[0] \n";
unshift @array4,99;
print "@array4 ---- $array4[0] \n";
unshift(@array4,'98');
print "@array4 ---- $array4[0] \n";
print "---------- array5 ----------\n";
@array5 = qw(eating rocks is wrong);
$array5 = "right";
print "@array5 ... $array5 \n";
print "this is $array5[3] \n";
print "this is ${array5}[3]\n";
print "this is $array5"."[3]\n";
print "this is $array5\[3] \n";
print "--------- for --------------\n";
for($i=0; $i<10; $i++){
print "for: $i\n";
}
#實際for和foreach是一樣的
#中斷循環對比C 的break和continue,Perl分別對應的是 last 和 next
#Perl中還特有 redo, 調到當前循環的頂端,不進行條件判斷就再次執行本次循環
#next會進入下一次循環,redo會繼續執行本次循環
print "--------- foreach ----------\n";
@rocks = qw(bedrock slate lava);
print "before rocks: \n @rocks \n";
foreach $rock (@rocks){
#如果有 redo 應該跳到的位置
$rock = "\t$rock";
$rock .= "\n";
print "new rock: $rock \n";
}
print "after rocks: \n @rocks \n";
print "--------- reverse 翻轉 ----------\n";
@reverse1 = reverse 6..9;
print "@reverse1 \n";
@reverse2 = reverse @reverse1;
print "@reverse2\n";
print "--------- sort 排序 -------------\n";
@sort1 = qw(aaa ccc eee bbb ddd);
print "@sort1\n";
@sort1 = sort(@sort1);
print "@sort1\n";
print "--------- context -----------\n";
print "變量和列表會根據上下文給出不同的值\n";
print 1+@sort1."\n";
@rocks = qw(xxx aaa ccc);
print "I have",@rocks,"rocks!\n";
#強制轉換為標量
print "I have", scalar @rocks, "rocks!\n";
print "--------- sub ----------------\n";
sub subtest1{
$temp=$a.$b;
$temp;
}
$a = "1";
$b = "2zzzzz";
$c = &subtest1;
print "c: $c\n";
sub addfunc{
$size = @_;
print "param size: $size\n";
my($num1, $num2);
($num1, $num2) = @_;
$num1 + $num2;
}
$addresult = &addfunc(100,99);
print "addresult: $addresult\n";
print "-------- 子程序 == 函數 ----\n";
print "函數參數放在 \@_數組內,可以用shift循環的切出第一個參數\n";
print "my() 定義局部變量\n";
print "子程序的返回值就是最后一部的計算值\n";
sub max{
my($maxValue) = shift @_;
foreach(@_){
if($_>$maxValue){
$maxValue = $_;
}
}
$maxValue;
}
@numset = (100,600,3000,3001);
$maxValue = &max(@numset);
print "maxValue: $maxValue\n";
sub which_element_is{
if(@_ < 2){
return "輸入參數個數不能小于2";
}
my($what, @array) = @_;
my $index = 0;
foreach(@array){
if($what eq $_){
#判斷字符串相等不能使用==,只能用eq
print "Find this element [$_]: $index\n";
return $index;
}
$index++;
}
return "can't find this element";
}
@names = qw(kevin xiang jie);
$which_element = &which_element_is("jie", @names);
print "which_element: $which_element\n";
print "上面的程序都是不嚴謹語法, 開頭添加 use strict 變為嚴謹語法檢測\n";
print '定義變量時需要用my定義, my $baa = 1; '."\n";
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。