Perl入門(數組)

來源:互聯網
上載者:User

 

#=====================================================

陣列變數以字元"@"打頭

@array = (1, 2, 3);

@var = (11, 27.1 , "a string");

#=====================================================

 

#=====================================================

數組間拷貝

@result = @original; 

 

用數組給列表賦值

@list1 = (2, 3, 4);

@list2 = (1, @list1, 5); # @list2 = (1, 2, 3, 4, 5)

 

數組對簡單變數的賦值

@array = (5, 7, 11);

($var1, $var2) = @array; # $var1 = 5, $var2 = 7, 11被忽略

 

@array = (5, 7);

($var1, $var2, $var3) = @array; # $var1 = 5, $var2 = 7, $var3 ="" (null)

#==================================================================================

 

#=====================================================

從標準輸入(STDIN)給變數賦值

$var = <STDIN>;

@array = <STDIN>; # ^D為結束輸入的符號

#=====================================================

 

#=====================================================

列表範圍: 

(1..10) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

(2, 5..7, 11) = (2, 5, 6, 7, 11)

(3..3) = (3)

用於實數

(2.1..5.3) = (2.1, 3.1 ,4.1, 5.1)

(4.5..1.6) = ()

用於字串

("aaa".."aad") = ("aaa","aab", "aac", "aad")

@day_of_month = ("01".."31")

可包含變數或運算式

($var1..$var2+5)

#=====================================================

 

#=====================================================

數組的輸出: 

@array = (1, 2, 3);

print (@array, "/n");

 結果為:

    123

@array = (1, 2, 3);

print ("@array/n");

  結果為:

    1 2 3

#=====================================================

 

 

#======================================================================================

列表/數組的長度 

  當陣列變數出現在預期簡單變數出現的地方,則PERL解譯器取其長度。

    @array = (1, 2, 3);

    $scalar = @array; # $scalar = 3,即@array的長度

    ($scalar) = @array; # $scalar = 1,即@array第一個元素的值

  註:以數組的長度為迴圈次數可如下編程:

    $count = 1;

    while ($count <= @array) {

    print ("element $count: $array[$count-1]/n");

    $count++;

    }

#======================================================================================

 

#======================================================================================    

子數組 

@array = (1, 2, 3, 4, 5);

@subarray = @array[0,1]; # @subarray = (1, 2)

@subarray2 = @array[1..3]; # @subarray2 = (2,3,4)

@array[0,1] = ("string", 46); # @array =("string",46,3,4,5) now 

@array[0..3] = (11, 22, 33, 44); # @array = (11,22,33,44,5) now

@array[1,2,3] = @array[3,2,4]; # @array = (11,44,33,5,5) now

@array[0..2] = @array[3,4]; # @array = (5,5,"",5,5) now

可以用子數組形式來交換元素:

@array[1,2] = @array[2,1];

#======================================================================================

 

#======================================================================================

7、有關數組的庫函數 

(1)sort--按字元順序排序

    @array = ("this", "is", "a","test");

    @array2 = sort(@array); # @array2 = ("a","is", "test", "this")

    @array = (70, 100, 8);

    @array = sort(@array); # @array = (100, 70, 8) now

(2)reverse--反轉數組

    @array2 = reverse(@array);

    @array2 = reverse sort (@array);

(3)chop--數組去尾

    chop的意義是去掉STDIN(鍵盤)輸入字串時最後一個字元--分行符號。而如果它作用到數組上,則將數組中每一個元素都做如此處理。

    @list = ("rabbit", "12345","quartz");

    chop (@list); # @list = ("rabbi", "1234","quart") now

(4)join/split--串連/拆分

    join的第一個參數是串連所用的中間字元,其餘則為待串連的字元數組。

    $string = join(" ", "this", "is","a", "string"); # 結果為"this is a string"

    @list = ("words","and");

    $string = join("::", @list, "colons"); #結果為"words::and::colons"

    @array = split(/::/,$string); #  @array = ("words","and", "colons") now

#======================================================================================

 

 

 

$count = 1;<br /> @array = ("my", "name", "is","wangbichao");<br /> while ($count <= @array)<br /> {<br /> print ("element $count: $array[$count-1]/n");<br /> $count++;<br /> } </p><p> $count = 1;<br /> @array = sort(@array); #按字元順序排序<br /> while ($count <= @array)<br /> {<br /> print ("element $count: $array[$count-1]/n");<br /> $count++;<br /> } </p><p> $count = 1;<br /> @array = reverse(@array); #反轉數組<br /> while ($count <= @array)<br /> {<br /> print ("element $count: $array[$count-1]/n");<br /> $count++;<br /> } </p><p> $count = 1;<br /> chop(@array); #數組每個標量去尾<br /> #@array = chop(@array);這樣寫的不能輸出的,我也不知道為什麼!<br /> while ($count <= @array)<br /> {<br /> print ("element $count: $array[$count-1]/n");<br /> $count++;<br /> } </p><p> $count = 1;<br /> $string = join("---",@array); #join的第一個參數是串連所用的中間字元,其餘則為待串連的字元數組。<br /> print("$string/n");<br /> while ($count <= @array)<br /> {<br /> print ("element $count: $array[$count-1]/n");<br /> $count++;<br /> } </p><p> print("$string/n");<br /> @array = split(/---/,$string); #把一個字串中含“---”的部分去掉,然後拆分成數組<br /> print("@array","/n");<br /> $count = 1;<br /> while($count <= @array)<br /> {<br /> print("elemrnt$count: $array[$count-1]/n");<br /> $count++;<br /> } 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.