明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

Perl教學(xué) 第12篇 Perl5中的引用之4

[摘要]五、多維數(shù)組語句@array = list;可以創(chuàng)建數(shù)組的引用,中括號可以創(chuàng)建匿名數(shù)組的引用。下面語句為用于畫圖的三維數(shù)組的例子:$line = ['solid' , 'black' , ['1','2','3'] ,...
五、多維數(shù)組
語句@array = list;可以創(chuàng)建數(shù)組的引用,中括號可以創(chuàng)建匿名數(shù)組的引用。下面語句為用于畫圖的三維數(shù)組的例子:
$line = ['solid' , 'black' , ['1','2','3'] , ['4','5','6']];
此語句建立了一個含四個元素的三維數(shù)組,變量$line指向該數(shù)組。前兩個元素是標(biāo)量,存貯線條的類型和顏色,后兩個元素是匿名數(shù)組的引用,存貯線條的起點和終點。訪問其元素語法如下:

$arrayReference->[$index] single-dimensional array
$arrayReference->[$index1][$index2] two-dimensional array
$arrayReference->[$index1][$index2][$index3] three-dimensional array
可以創(chuàng)建在你的智力、設(shè)計經(jīng)驗和計算機的內(nèi)存允許的情況下極盡復(fù)雜的結(jié)構(gòu),但最好對可能讀到或管理你的代碼的人友好一些--盡量使代碼簡單些。另一方面,如果你想向別人炫耀你的編程能力,Perl給你足夠的機會和能力編寫連自己都難免糊涂的代碼。:)
建議:當(dāng)你想使用多于三維的數(shù)組時,最好考慮使用其它數(shù)據(jù)結(jié)構(gòu)來簡化代碼。
下面為創(chuàng)建和使用二維數(shù)組的例子:

1 #!/usr/bin/perl
2 #
3 # Using Multi-dimensional Array references
4 #
5 $line = ['solid', 'black', ['1','2','3'] , ['4', '5', '6']];
6 print "\$line->[0] = $line->[0] \n";
7 print "\$line->[1] = $line->[1] \n";
8 print "\$line->[2][0] = $line->[2][0] \n";
9 print "\$line->[2][1] = $line->[2][1] \n";
10 print "\$line->[2][2] = $line->[2][2] \n";
11 print "\$line->[3][0] = $line->[3][0] \n";
12 print "\$line->[3][1] = $line->[3][1] \n";
13 print "\$ine->[3][2] = $line->[3][2] \n";
14 print "\n"; # The obligatory output beautifier.
結(jié)果輸出如下:

$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
那么三維數(shù)組又如何呢?下面是上例略為改動的版本。

1 #!/usr/bin/perl
2 #
3 # Using Multi-dimensional Array references again
4 #
5 $line = ['solid', 'black', ['1','2','3', ['4', '5', '6']]];
6 print "\$line->[0] = $line->[0] \n";
7 print "\$line->[1] = $line->[1] \n";
8 print "\$line->[2][0] = $line->[2][0] \n";
9 print "\$line->[2][1] = $line->[2][1] \n";
10 print "\$line->[2][2] = $line->[2][2] \n";
11 print "\$line->[2][3][0] = $line->[2][3][0] \n";
12 print "\$line->[2][3][1] = $line->[2][3][1] \n";
13 print "\$line->[2][3][2] = $line->[2][3][2] \n";
14 print "\n";
結(jié)果輸出如下:

$line->[0] = solid
$line->[1] = black
$line->[2][0] = 1
$line->[2][1] = 2
$line->[2][2] = 3
$line->[2][3][0] = 4
$line->[2][3][1] = 5
$line->[2][3][2] = 6
訪問第三層元素的方式形如$line->[2][3][0],類似于C語言中的Array_pointer[2][3][0]。本例中,下標(biāo)均為數(shù)字,當(dāng)然亦可用變量代替。用這種方法可以把數(shù)組和哈希表結(jié)合起來構(gòu)成復(fù)雜的結(jié)構(gòu),如下:

1 #!/usr/bin/perl
2 #
3 # Using Multi-dimensional Array and Hash references
4 #
5 %cube = (
6 '0', ['0', '0', '0'],
7 '1', ['0', '0', '1'],
8 '2', ['0', '1', '0'],
9 '3', ['0', '1', '1'],
10 '4', ['1', '0', '0'],
11 '5', ['1', '0', '1'],
12 '6', ['1', '1', '0'],
13 '7', ['1', '1', '1']
14 );
15 $pointer = \%cube;
16 print "\n Da Cube \n";
17 foreach $i (sort keys %$pointer) {
18 $list = $$pointer{$i};
19 $x = $list->[0];
20 $y = $list->[1];
21 $z = $list->[2];
22 printf " Point $i = $x,$y,$z \n";
23 }
結(jié)果輸出如下:

Da Cube
Point 0 = 0,0,0
Point 1 = 0,0,1
Point 2 = 0,1,0
Point 3 = 0,1,1
Point 4 = 1,0,0
Point 5 = 1,0,1
Point 6 = 1,1,0
Point 7 = 1,1,1
這是一個定義立方體的例子。%cube中保存的是點號和坐標(biāo),坐標(biāo)是個含三個數(shù)字的數(shù)組。變量$list獲取坐標(biāo)數(shù)組的引用:$list = $$ pointer{$i}; 然后訪問各坐標(biāo)值:$x = $list->[0]; ... 也可用如下方法給$x、$y和$z賦值:($x,$y,$z) = @$list;
使用哈希表和數(shù)組時,用$和用->是類似的,對數(shù)組而言下面兩個語句等效:
$$names[0] = "kamran";
$names->[0] = "kamran";
對哈希表而言下面兩個語句等效:
$$lastnames{"kamran"} = "Husain";
$lastnames->{"kamran"} = "Husain";
Perl中的數(shù)組可以在運行中創(chuàng)建和擴展。當(dāng)數(shù)組的引用第一次在等式左邊出現(xiàn)時,該數(shù)組自動被創(chuàng)建,簡單變量和多維數(shù)組也是一樣。如下句,如果數(shù)組contours不存在,則被創(chuàng)建:
$contours[$x][$y][$z] = &xlate($mouseX, $mouseY);