perl陣列硬引用 Perl陣列和引用使用指導

2021-10-12 15:43:27 字數 3715 閱讀 8530

本文和大家重點討論一下perl陣列和引用的概念,perl中的perl陣列和雜湊表始終是一維的。因此,perl陣列和雜湊表只儲存標量值,不直接存貯perl陣列或其它的複雜資料結構。perl陣列的成員要麼是數(或字串)要麼是引用。

一、perl陣列和引用

關於perl語言應該記住的最重要的一點可能是:perl中的perl陣列和雜湊表始終是一維的。因此,perl陣列和雜湊表只儲存標量值,不直接存貯perl陣列或其它的複雜資料結構。perl陣列的成員要麼是數(或字串)要麼是引用。

對perl陣列和雜湊表可以象對簡單變數一樣使用反斜線操作符,perl陣列的引用如下:

1#!/usr/bin/perl

2#3#usingarrayreferences

4#5$pointer=\@ar**;

6printf"\npointeraddressofar**=$pointer\n";

7$i=scalar(@$pointer);

8printf"\nnumberofarguments:$i\n";

9$i=0;

10foreach(@$pointer)

執行結果如下:

$test1234

pointeraddressofar**=array(0x806c378)

numberofarguments:4

0:1;

1:2;

2:3;

3:4;第5行將引用$pointer指向perl陣列@ar**,第6行輸出ar**的位址。$pointer返回perl陣列***個元素的位址,這與c語言中的perl陣列指標是類似的。第7行呼叫函式scalar()獲得perl陣列的元素個數,該引數亦可為@ar**,但用指標則必須用@$pointer的形式指定其型別為perl陣列,$pointer給出位址,@符號說明傳遞的位址為perl陣列的***個元素的位址。第10行與第7行類似,第11行用形式$$pointer[$i]列出所有元素。

對關聯perl陣列使用反斜線操作符的方法是一樣的--把所有關聯perl陣列名換成引用$poniter。注意perl陣列和簡單變數(標量)的引用顯示時均帶有型別--array和scalar,雜湊表(關聯perl陣列)和函式也一樣,分別為hash和code。

與perl陣列類似,通過引用訪問雜湊表的元素形式為$$pointer,當然,$index是雜湊表的鍵值,而不僅是數字。還有幾種訪問形式,此外,構建雜湊表還可以用=>操作符,可讀性更好些。下面再看乙個例子:

1#!/usr/bin/perl

2#3#usingarrayreferences

4#5%weekday=(

6'01'=>'mon',

7'02'=>'tue',

8'03'=>'wed',

9'04'=>'thu',

10'05'=>'fri',

11'06'=>'sat',

12'07'=>'sun',

13);

14$pointer=\%weekday;

15$i='05';

16printf"\n***************===starttest***************==\n";

17#18#thesenexttwolinesshouldshowanoutput

19#20printf'$$pointeris';

21printf"$$pointer\n";

22printf'$is';

23printf"$\n";

24printf'$pointer->is';

26printf"$pointer->\n";

27#28#thesenexttwolinesshouldnotshowanything29#

30printf'$}is';

31printf"$}\n";

32printf'$}is';

33printf"$}";

34printf"\n***************===endoftest***************==\n";

結果輸出如下:

***************===starttest***************==

$$pointerisfri

$isfri

$pointer->isfri

$}is

$}is

***************===endoftest***************==

可以看到,前三種形式的輸出顯示了預期的結果,而後兩種則沒有。當你不清楚是否正確時,就輸出結果看看。在perl中,有不明確的**就用print語句輸出來實驗一下,這能使你清楚perl是怎樣解釋你的**的。

二、多維perl陣列

語句@array=list;可以建立perl陣列的引用,中括號可以建立匿名perl陣列的引用。下面語句為用於畫圖的三維perl陣列的例子:

$line=['solid','black',['1','2','3'],['4','5','6']];

此語句建立了乙個含四個元素的三維perl陣列,變數$line指向該perl陣列。前兩個元素是標量,存貯線條的型別和顏色,後兩個元素是匿名perl陣列的引用,存貯線條的起點和終點。訪問其元素語法如下:

$arrayreference->[$index]single-dimensionalarray

$arrayreference->[$index1][$index2]two-dimensionalarray

$arrayreference->[$index1][$index2][$index3]three-dimensionalarray

可以建立在你的智力、設計經驗和計算機的記憶體允許的情況下極盡複雜的結構,但***對可能讀到或管理你的**的人友好一些--盡量使**簡單些。另一方面,如果你想向別人炫耀你的程式設計能力,perl給你足夠的機會和能力編寫連自己都難免糊塗的**。:)

建議:當你想使用多於三維的perl陣列時,***考慮使用其它資料結構來簡化**。

下面為建立和使用二維perl陣列的例子:

1#!/usr/bin/perl

2#3#usingmulti-dimensionalarrayreferences

4#5$line=['solid','black',['1','2','3'],['4','5','6']];

6print"\$line->[0]=$line->[0]\n";

7print"\$line->[1]=$line->[1]\n";

8print"\$line->[2][0]=$line->[2][0]\n";

9print"\$line->[2][1]=$line->[2][1]\n";

10print"\$line->[2][2]=$line->[2][2]\n";

11print"\$line->[3][0]=$line->[3][0]\n";

12print"\$line->[3][1]=$line->[3][1]\n";

13print"\$line->[3][2]=$line->[3][2]\n";

14print"\n";#theobligatoryoutputbeautifier

結果輸出如下:

$line->[0]=solid

$line->[1]=black

$line->[2][0]=1

$line->[2][1]=2

$line->[2][2]=3

$line->[3][0]=4

$line->[3][1]=5

$line->[3][2]=6

點讚 0

perl陣列硬引用 perl中的引用

為什麼使用引用?在perl4中,hash表中的value欄位只能是scalar,而不能是list,這對於有些情況是很不方便的,比如有下面的資料 chicago,usa frankfurt,germany berlin,germany washington,usa helsinki,finland n...

Perl入門(三)Perl的陣列

perl陣列的宣告方式 perl使用 符號宣告乙個陣列 array 使用 或 qw 宣告陣列中元素 乙個完整的宣告方式為 array 12,123 abc var 方法一 或者 array qw 12 123 abc 方法二 或者 array 1.10 方法三 perl陣列的訪問 陣列通過下標進行訪...

Perl陣列排序

本文我們學習如何用perl對字串或者數字陣列進行排序。perl有個內建函式叫做sort毫無疑問的可以排序乙個陣列。其最簡單的形式是傳遞乙個陣列,它會返回排序後的元素組成的陣列。sorted sort original。usr bin perl usestrict use warnings use 5...