1. 程式人生 > >【perl】列表,陣列和雜湊的巢狀

【perl】列表,陣列和雜湊的巢狀

Ⅰ. 列表巢狀列表(列表巢狀列表即為二維列表)

a. 列表的宣告

方法一:

@list = (

["banana","apple","orange","pear"],

["cauliflower","lettuce","tomato","cucumber"],

["orange juice","apple juice","red tea","red wine"]

);

 

方法二:

@fruit = ("banana","apple","orange","pear");

@vegetable = ("cauliflower","lettuce","tomato","cucumber");

@drink = ("orange juice","apple juice","red tea","red wine");

 

正確方法:

@list = ([@fruit],[@vegetable],[@drink]);

錯誤方法:

@list = (@fruit, @vegetable, @drink);

錯誤剖析:如果不用[]把每個子列表括起來,那麼這個列表@list就相當於@fruit, @vegetable, @drink這三個列表拼接而成的一個一維列表相當於@list  = ("banana","apple","orange","pear","cauliflower","lettuce","tomato","cucumber","orange juice","apple juice","red tea","red wine")

 

方法三:

正確方法:

$list[0] = ["banana","apple","orange","pear"];

錯誤方法:

$list[0] = ("banana","apple","orange","pear");

錯誤剖析:這樣的結果是$list[0] = "pear" 並且會報警告

 

b. 列表的訪問

$list[0][0] = "banana";

#$list[0] and @list[0] 兩種表示都可以,但是 @list[0] 會報警告

 

 

Ⅱ. 列表巢狀雜湊

a. 列表的宣告

方法一:

@list = (

{

'fruit' => 'banana',

'vegetable' => 'cauliflower',

'drink' => 'orange juice'

},

{

'fruit' => 'apple',

'vegetable' => 'lettuce',

'drink' => 'apple juice'

},

{

'fruit' => 'orange',

'vegetable' => 'tomato',

'drink' => 'red tea'

},

{

'fruit' => 'pear',

'vegetable' => 'cucumber',

'drink' => 'red wine'

}

);

 

方法二:

%group1 = (

'fruit' => 'banana',

'vegetable' => 'cauliflower',

'drink' => 'orange juice'

);

%group2 = (

'fruit' => 'apple',

'vegetable' => 'lettuce',

'drink' => 'apple juice'

);

%group3 = (

'fruit' => 'orange',

'vegetable' => 'tomato',

'drink' => 'red tea'

);

%group4 = (

'fruit' => 'pear',

'vegetable' => 'cucumber',

'drink' => 'red wine'

);

 

正確方法:

@list = ({%group1}, {%group2}, {%group3}, {%group4});

錯誤方法:

@list = (%group1, %group2, %group3, %group4);

錯誤剖析:如果不用{}把每個子列表括起來,那麼這個列表@list就相當於%group1, %group2, %group3, %group4 這四個雜湊拼接而成的一個一維列表,相當於@list = ( 'fruit', 'banana', 'drink', 'orange juice', 'vegetable', 'cauliflower', 'fruit', 'apple', 'drink', 'apple juice', 'vegetable', 'lettuce', 'fruit', 'orange', 'drink', 'red tea', 'vegetable', 'tomato', 'fruit', 'pear', 'drink', 'red wine', 'vegetable', 'cucumber')

 

方法三:

正確方法:

$list[0] = {'fruit' => 'banana', 'vegetable' => 'cauliflower','drink' => 'orange juice'};

$list[0] = {%group1};

錯誤方法:

$list[0] = ('fruit' => 'banana', 'vegetable' => 'cauliflower','drink' => 'orange juice');

錯誤剖析:這樣的結果是$list[0] = 'orange juice' 並且有警告

$list[0] = ['fruit' => 'banana', 'vegetable' => 'cauliflower','drink' => 'orange juice'] 

錯誤剖析:這樣的結果是$list[0] = ['fruit','banana','vegetable','cauliflower','drink','orange juice']

 

b. 列表的訪問

$list[0]{'fruit'} = 'banana' ;
$list[0]{fruit}= 'banana'; #兩者都可以

$list[0] = {'fruit' => 'banana','drink' => 'orange juice','vegetable' => 'cauliflower'};

 

Ⅲ. 雜湊巢狀雜湊

a. 雜湊的宣告

方法一:

my %hash = (

group1 => {

'fruit' => 'banana',

'vegetable' => 'cauliflower',

'drink' => 'orange juice'

},

group2 => {

fruit => 'apple',

vegetable => 'lettuce',

drink => 'apple juice'

},

group3 => {

'fruit' => 'orange',

'vegetable' => 'tomato',

'drink' => 'red tea'

},

group4 => {

fruit => 'pear',

vegetable => 'cucumber',

drink => 'red wine'

}

);
%hash = (

'group1', {'fruit', 'banana', 'drink', 'orange juice', 'vegetable', 'cauliflower'},

'group2', {'fruit', 'apple', 'drink', 'apple juice', 'vegetable', 'lettuce'},

'group3', {'fruit', 'orange', 'drink', 'red tea', 'vegetable', 'tomato'},

'group4', {'fruit', 'pear', 'drink', 'red wine', 'vegetable', 'cucumber'}

);

 

方法二:

%group1 = (

'fruit' => 'banana',

'vegetable' => 'cauliflower',

'drink' => 'orange juice'

);

%group2 = (

'fruit' => 'apple',

'vegetable' => 'lettuce',

'drink' => 'apple juice'

);

%group3 = (

'fruit' => 'orange',

'vegetable' => 'tomato',

'drink' => 'red tea'

);

%group4 = (

'fruit' => 'pear',

'vegetable' => 'cucumber',

'drink' => 'red wine'

);

 

正確方法:

%hash = (

'group1' => {%group1},

'group2' => {%group2},

'group3' => {%group3},

'group4' => {%group4}

);

錯誤方法:

%hash = (%group1, %group2, %group3, %group4); 

錯誤剖析:這個雜湊相當於%hash = %group4

%hash = (

'group1' => %group1,

'group2' => %group2,

'group3' => %group3,

'group4' => %group4

); 

錯誤剖析:這個雜湊的結果相當於 %hash = ('tomato','group4','orange juice','vegetable','cauliflower','group2','orange','drink','fruit','pear','group1','fruit','banana','drink','drink','red wine','vegetable','cucumber','group3','fruit','red tea','vegetable')

 

方法三:

$hash{group1} = {%group1};

$hash{'group1'} = {

'fruit' => 'banana',

'vegetable' => 'cauliflower',

'drink' => 'orange juice'

};

$hash{'group1'} = {'fruit', 'banana', 'vegetable', 'cauliflower', 'drink', 'orange juice'};

b. 雜湊的訪問

$hash{'group1'}{'fruit'} = 'banana' and  $hash{group1}{fruit} are the same.

$list{group1) = {'fruit' => 'banana','drink' => 'orange juice','vegetable' => 'cauliflower'};

 

Ⅳ. 雜湊巢狀列表

a. 雜湊的宣告

方法一:

%hash = (

'fruit' => ["banana","apple","orange","pear"],

'vegetable' => ["cauliflower","lettuce","tomato","cucumber"],

'drink' => ["orange juice","apple juice","red tea","red wine"]

);



%hash = (

'fruit', ["banana","apple","orange","pear"],

'vegetable', ["cauliflower","lettuce","tomato","cucumber"],

'drink', ["orange juice","apple juice","red tea","red wine"]

);

 

方法二:

@fruit = ("banana","apple","orange","pear");

@vegetable = ("cauliflower","lettuce","tomato","cucumber");

@drink = ("orange juice","apple juice","red tea","red wine");



%hash = (

'fruit', [@fruit],

'vegetable', [@vegetable],

'drink', [@vegetable]

);

 

方法三:

$hash{fruit} = [@fruit];

$hash{'fruit'} = ["banana","apple","orange","pear"];

 

b. 雜湊的訪問

$hash{'fruit'}[0] = 'banana';

$hash{'fruit'} = ['banana','apple','orange','pear'];