9.1.1 创建和访问一个两维数组 [root@wx03 4]# cat a1.pl @Aoa = ( ["fred", "barney" ], ["george", "jane", "elroy" ], ["homer", "marge", "bart" ], ); print $Aoa[0]->[1]; print " "; [root@wx03 4]# perl a1.pl barney $Aoa[0]->[1]; [0]表示第一个元素,->[1]表示数组引用的第2个元素 请注意 $array[3] 和 $array->[3] 是不一样的。第一个东西讲的是 @array 里的第四个元素, 而第二个东西讲 的是一个保存在 $array 里的数组(可能是匿名的数组)引用的第四个元素。 [root@wx03 4]# cat a2.pl $ref_to_AoA = [ [ "fred", "barney674128462", "pebbles", "bamm bamm", "dino", ], [ "homer", "bart", "marge", "maggie", ], [ "george", "jane", "elroy", "judy", ], ]; print $ref_to_AoA->[0]->[1]; print " "; [root@wx03 4]# perl a2.pl barney674128462 请记住在每一对相邻的花括弧或方括弧之间有一个隐含的 ->。因此下面两行: [root@wx03 4]# cat tmp.out a b56757 c e f g o p q r s t [root@wx03 4]# cat a3.pl open (LOG ,"<","tmp.out"); while (<LOG>) { @tmp = split; push @AoA,[@tmp]; }; use Data::Dumper; $str=Dumper(@AoA); print @AoA; print " "; print $str; print " "; print $AoA[0][1]; print " "; [root@wx03 4]# perl a3.pl ARRAY(0xf12338)ARRAY(0xf12410)ARRAY(0xf12cf8)ARRAY(0xf20088)ARRAY(0xefe160) $VAR1 = [ 'a', 'b56757', 'c' ]; $VAR2 = [ 'e', 'f', 'g' ]; $VAR3 = [ 'o', 'p', 'q' ]; $VAR4 = [ 'r', 's', 't' ]; $VAR5 = []; b56757 [root@wx03 4]# cat a4.pl sub func{ $a=shift; $b=shift; return $a + $b; }; for $x (0..3){ # 对每一行... for $y (0..3) { # 对每一列... $AoA[$x][$y] = func($x, $y); # ...设置调用 } }; print @AoA; use Data::Dumper; $str=Dumper(@AoA); print "$str is $str "; [root@wx03 4]# perl a4.pl ARRAY(0xec3368)ARRAY(0xec3488)ARRAY(0xed0158)ARRAY(0xee4d10)$str is $VAR1 = [ 0, 1, 2, 3 ]; $VAR2 = [ 1, 2, 3, 4 ]; $VAR3 = [ 2, 3, 4, 5 ]; $VAR4 = [ 3, 4, 5, 6 ]; for $i (1..10) { @array = ($i,$i+1,$i+2); $AoA[$i] = [ @array ]; }; print @AoA; ~ $AoA[$i] = [ @array ]; ##数组的每个元素是一个数组引用 [root@wx03 4]# cat a5.pl for $i (1..3) { @array = ($i,$i+1,$i+2); $AoA[$i] = [ @array ]; # 错误! }; print @AoA; use Data::Dumper; $str=Dumper(@AoA); print "$str is $str "; [root@wx03 4]# perl a5.pl ARRAY(0xe7f368)ARRAY(0xe7f4a0)ARRAY(0xe8d0b8)$str is $VAR1 = undef; $VAR2 = [ 1, 2, 3 ]; $VAR3 = [ 2, 3, 4 ]; $VAR4 = [ 3, 4, 5 ]; 9.2 数组的散列 [root@wx03 4]# cat a6.pl %HoA = ( flintstones => [ "fred", "barney" ], jetsons => [ "george", "jane", "elroy" ], simpsons => [ "homer", "marge", "bart" ], ); print $HoA{flintstones}->[1]; print " "; ##添加元素 # $HoA{test}=['a','b']; print %HoA; print " "; [root@wx03 4]# perl a6.pl barney flintstonesARRAY(0xfb0160)jetsonsARRAY(0xfc4368)simpsonsARRAY(0xfc44b8)testARRAY(0xfc4338) [root@wx03 4]# cat a7.pl open (LOG ,"<","t1.out"); while ( $line = <LOG> ) { ($who, $rest) = split /:S*/, $line, 2; print "$who is $who "; print "$rest is $rest "; @fields = split /s+/, $rest; $HoA{$who} = [ @fields ]; }; print %HoA; print " "; [root@wx03 4]# cat a7.pl open (LOG ,"<","t1.out"); @fields=(); while ( $line = <LOG> ) { ($who, $rest) = split /:S*/, $line, 2; print "$who is $who "; print "$rest is $rest "; @fields = split /#/, $rest; print "@fields is @fields "; $HoA{$who} = [ @fields ]; }; print %HoA; print " "; use Data::Dumper; $str=Dumper(%HoA); print "$str is $str "; print $HoA{flintsotnes}->[1]; print " "; [root@wx03 4]# perl a7.pl $who is flintsotnes $rest is fred#barney#wilma#dino @fields is fred barney wilma dino $who is jetsons $rest is george#jane#elroy @fields is george jane elroy $who is simpsons $rest is homer#marge#bart @fields is homer marge bart simpsonsARRAY(0x2297668)flintsotnesARRAY(0x228a368)jetsonsARRAY(0x228a338) $str is $VAR1 = 'simpsons'; $VAR2 = [ ' homer', 'marge', 'bart ' ]; $VAR3 = 'flintsotnes'; $VAR4 = [ ' fred', 'barney', 'wilma', 'dino ' ]; $VAR5 = 'jetsons'; $VAR6 = [ ' george', 'jane', 'elroy ' ]; barney [root@wx03 4]# perl a7.pl $who is flintsotnes $rest is fred barney wilma dino $who is jetsons $rest is george jane elroy $who is simpsons $rest is homer marge bart flintsotnesARRAY(0x1c0a350)simpsonsARRAY(0x1c17d50)jetsonsARRAY(0x1c0a368) 散列的数组: [root@wx03 4]# cat a8.pl @AoH = ( { husband => "barney", wife => "betty", son => "bamm bamm", }, { husband => "george", wife => "jane", son => "elroy", }, { husband => "homer", wife => "marge", son => "bart", }, ); print $AoH[0]->{wife}; print " "; [root@wx03 4]# perl a8.pl betty 生产散列的数组: [root@wx03 4]# cat t1.pl open (LOG ,"<","t2.out"); while (<LOG>) { $rec = {}; for $field ( split ) { ($key, $value) = split /=/, $field; $rec->{$key} = $value; } push @AoH, $rec; } print @AoH; print " "; use Data::Dumper; $str=Dumper(@AoH); print "$str is $str "; [root@wx03 4]# cat t2.out husband=fred friend=barney [root@wx03 4]# perl t1.pl HASH(0x1e82160)HASH(0x1e96338) $str is $VAR1 = { 'husband' => 'fred' }; $VAR2 = { 'friend' => 'barney' }; 9.4 散列的散列: 9.4.1 构成一个散列的散列 [root@wx03 4]# cat t2.pl %HoH = ( flintstones => { husband => "fred", pal => "barney", }, jetsons => { husband => "george", wife => "jane", "his boy" => "elroy", # 键字需要引号 }, simpsons => { husband => "homer", wife => "marge", kid => "bart", }, ); print $HoH{flintstones}->{pal}; print " "; [root@wx03 4]# perl t2.pl barney 9.4.2 生成散列的散列 下面 [root@wx03 4]# cat t3.pl open (LOG ,"<","t3.out"); while( <LOG> ){ $who = flintstones; for $field ( split ) { ($key, $value) = split /=/, $field; $HoH{$who}{$key} = $value; } }; print %HoH; use Data::Dumper; $str=Dumper(%HoH); print "$str is $str "; print $HoH{flintstones}{wife}; print " "; [root@wx03 4]# cat t3.out husband=fred pal=barney wife=wilma pet=dino [root@wx03 4]# perl t3.pl flintstonesHASH(0xb6e320)$str is $VAR1 = 'flintstones'; $VAR2 = { 'pal' => 'barney', 'husband' => 'fred', 'pet' => 'dino', 'wife' => 'wilma' }; wilma 9.5 函数的散列 %HoF = ( # Compose a hash of functions exit => sub { exit }, help => &show_help, watch => sub { $watch = 1 }, mail => sub { mail_msg($msg) }, edit => sub { $edited++; editmsg($msg); }, delete => &confirm_kill, );