数据结构

创建和访问一个两维数组:

# 给一个数组赋予一个数组引用列表。

[root@wx03 4]# cat t1.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

print $Aoa[0]->[1];
print "\n";
[root@wx03 4]# perl t1.pl 
barney

如果你想要一个指向数组的引用,那么你要使用方括弧:

# 创建一个指向一个数组的数组的引用。

[root@wx03 4]# cat t2.pl 
$ref_to_AoA = [
[ "fred", "barney", "pebbles", "bamm bamm", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "elroy", "judy", ],
];
print $ref_to_AoA->[0]->[1];
print "\n";

[root@wx03 4]# perl t2.pl 
barney

请记住在每一对相邻的花括弧或方括弧之间有一个隐含的 ->。因此下面两行:

$AoA[2][3]
$ref_to_AoA->[2][3]
等效于下面两行:

$AoA[2]->[3]
$ref_to_AoA->[2]->[3]

自行生长:

[root@wx03 4]# cat t4.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

print $Aoa[0]->[1];
print "\n";
print @Aoa ;
print "\n";


open (LOG ,"<","tmp.out");  
while (<LOG>) {
@tmp = split; # 把元素分裂成一个数组
push @Aoa, [ @tmp ]; # 向 @AoA 中增加一个匿名数组引用
};
print @Aoa ;
print "\n";
print $Aoa[3]->[1];
[root@wx03 4]# perl t4.pl 
barney
ARRAY(0x2521160)ARRAY(0x2535368)ARRAY(0x25354b8)
ARRAY(0x2521160)ARRAY(0x2535368)ARRAY(0x25354b8)ARRAY(0x2543260)
b2[root@wx03 4]# 


给数组增加新的列:
[root@wx03 4]# cat t5.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

$ref_to_Aoa=\@Aoa;
for $x (0..2) { # 对每一行...
$ref_to_Aoa->[$x][3] = $x; # ...设置第四行
};

print $ref_to_Aoa->[1]->[3];
print "\n";
[root@wx03 4]# perl t5.pl 
1


/**************************

[root@wx03 4]# cat t5.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

use Data::Dumper;
$str=Dumper(@Aoa);
print "\$str is $str\n";

$ref_to_Aoa=\@Aoa;

use Data::Dumper;
$str=Dumper($ref_to_Aoa);
print "\$str is $str\n";


for $x (0..2) { # 对每一行...
$ref_to_Aoa->[$x][3] = $x; # ...设置第四行
};

print $ref_to_Aoa->[1]->[3];
print "\n";


use Data::Dumper;
$str=Dumper($ref_to_Aoa);
print "\$str is $str\n";
[root@wx03 4]# perl t5.pl 
$str is $VAR1 = [
          'fred',
          'barney'
        ];
$VAR2 = [
          'george',
          'jane',
          'elroy'
        ];
$VAR3 = [
          'homer',
          'marge',
          'bart'
        ];

$str is $VAR1 = [
          [
            'fred',
            'barney'
          ],
          [
            'george',
            'jane',
            'elroy'
          ],
          [
            'homer',
            'marge',
            'bart'
          ]
        ];

1
$str is $VAR1 = [
          [
            'fred',
            'barney',
            undef,
            0
          ],
          [
            'george',
            'jane',
            'elroy',
            1
          ],
          [
            'homer',
            'marge',
            'bart',
            2
          ]
        ];

访问和打印:


下面的代码打印整个结构,循环遍历
@AoA 的元素并且在 print 语句里对每个元素进行解引用:

[root@wx03 4]# cat t6.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

print $Aoa[2][2];
print "\n";
print @Aoa;
print "\n";

for $row (@Aoa) {
print "@$row\n";
}
[root@wx03 4]# perl t6.pl 
bart
ARRAY(0x1239160)ARRAY(0x124d368)ARRAY(0x124d4b8)
fred barney
george jane elroy
homer marge bart


[root@wx03 4]# cat t6.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

print $Aoa[2][2];
print "\n";
print @Aoa;
print "\n";

for $row (@Aoa) {
print "@$row\n";
};

for $i (0..$#Aoa) {
print "row $i is: @{$Aoa[$i]}\n";
}
[root@wx03 4]# perl t6.pl 
bart
ARRAY(0x1083160)ARRAY(0x1097368)ARRAY(0x10974b8)
fred barney
george jane elroy
homer marge bart
row 0 is: fred barney
row 1 is: george jane elroy
row 2 is: homer marge bart

9.1.4  片段:


如果你想访问一个多维数组的某个片段(一行的一部分),你就是在准备做一些奇特的脚标处理。


[root@wx03 4]# cat t7.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

@part=();

for ($y=0;$y<3;$y++){
push @part,$Aoa[2]->[$y];
};
print @part;
print "\n";
[root@wx03 4]# perl t7.pl 
homermargebart


[root@wx03 4]# cat t7.pl 
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);

@newAoA = ();
for ($startx = $x = 0; $x <= 2; $x++) {
for ($starty = $y=0; $y <= 1; $y++) {
$newAoA[$x - $startx][$y - $starty] = $Aoa[$x][$y];
}
};
print @newAoA;
print "\n";

for $row (@newAoA) {
print "@$row\n";
};
[root@wx03 4]# perl t7.pl 
ARRAY(0x1fb24a0)ARRAY(0x1fbf158)ARRAY(0x1fbf1b8)
fred barney
george jane
homer marge



[root@wx03 4]# cat t8.pl 
for $i (1..4) {
@array = ($i,$i+1,$i+2);
$AoA[$i] = [@array]; 
};

print @AoA;
print "\n";
use Data::Dumper;
$str=Dumper(@AoA);
print "\$str is $str\n";
[root@wx03 4]# perl t8.pl 
ARRAY(0xdee368)ARRAY(0xdee4a0)ARRAY(0xdfbfd0)ARRAY(0xe20360)
$str is $VAR1 = undef;
$VAR2 = [
          1,
          2,
          3
        ];
$VAR3 = [
          2,
          3,
          4
        ];
$VAR4 = [
          3,
          4,
          5
        ];
$VAR5 = [
          4,
          5,
          6
        ];




9.2 数组的散列:

你可以用下面的方法创建一个匿名数组的散列:
# 如果键字是标识符,我们通常省略引号


[root@wx03 4]# cat t9.pl 
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);

print $HoA{simpsons}->[2];
print "\n";
[root@wx03 4]# perl t9.pl 
bart

要向散列增加另外一个数组,你可以简单地说:

[root@wx03 4]# cat t9.pl 
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);

print $HoA{simpsons}->[2];
print "\n";
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];


print %HoA;
print "\n";

use Data::Dumper;
$str=Dumper(%HoA);
print "\$str is $str\n";
[root@wx03 4]# perl t9.pl 
bart
simpsonsARRAY(0xed84b8)jetsonsARRAY(0xed8368)teletubbiesARRAY(0xed8338)flintstonesARRAY(0xec4160)
$str is $VAR1 = 'simpsons';
$VAR2 = [
          'homer',
          'marge',
          'bart'
        ];
$VAR3 = 'jetsons';
$VAR4 = [
          'george',
          'jane',
          'elroy'
        ];
$VAR5 = 'teletubbies';
$VAR6 = [
          'tinky winky',
          'dipsy',
          'laa-laa',
          'po'
        ];
$VAR7 = 'flintstones';
$VAR8 = [
          'fred',
          'barney'
        ];



9.2.2  生成数组的散列:

[root@wx03 4]# cat t10.pl 
my $line="flintsotnes: fred barney wilma dino";
($who, $rest) = split /:\S*/, $line;
print "\$who is $who\n";
print "\$rest is $rest\n";

[root@wx03 4]# perl t10.pl 
$who is flintsotnes
$rest is  fred barney wilma dino


[root@wx03 4]# cat t11.pl 
open (LOG ,"<","a.txt");  
while ( $line = <LOG> ) {
($who, $rest) = split /:\S*/, $line;
@fields = split "\S+",$rest;
$HoA{$who} = [ @fields ];
};
print %HoA;
print "\n";
use Data::Dumper;
$str=Dumper(%HoA);
print "\$str is $str\n";
[root@wx03 4]# perl t11.pl 
flintsotnesARRAY(0x10fb310)simpsonsARRAY(0x110f3f8)jetsonsARRAY(0x110f338)
$str is $VAR1 = 'flintsotnes';
$VAR2 = [
          ' fred barney wilma dino
'
        ];
$VAR3 = 'simpsons';
$VAR4 = [
          ' homer marge bart
'
        ];
$VAR5 = 'jetsons';
$VAR6 = [
          ' george jane elroy
'
        ];


9.2.3  访问和打印数组的散列:

可以打印所有这些家族,方法是遍历该散列的所有键字:

遍历hash;

[root@wx03 4]# cat t11.pl 
open (LOG ,"<","a.txt");  
while ( $line = <LOG> ) {
($who, $rest) = split /:\S*/, $line;
@fields = split "\S+",$rest;
$HoA{$who} = [ @fields ];
};
print %HoA;
print "\n";
use Data::Dumper;
$str=Dumper(%HoA);
print "\$str is $str\n";


for $family ( keys %HoA ){
print "$family: @{ $HoA{$family} }\n";
}
[root@wx03 4]# perl t11.pl 
simpsonsARRAY(0x15153f8)flintsotnesARRAY(0x1501310)jetsonsARRAY(0x1515338)
$str is $VAR1 = 'simpsons';
$VAR2 = [
          ' homer marge bart
'
        ];
$VAR3 = 'flintsotnes';
$VAR4 = [
          ' fred barney wilma dino
'
        ];
$VAR5 = 'jetsons';
$VAR6 = [
          ' george jane elroy
'
        ];

simpsons:  homer marge bart

flintsotnes:  fred barney wilma dino

jetsons:  george jane elroy


9.3  散列的数组:

9.3.1  组成一个散列的数组
你可以用下面方法创建一个匿名散列的数组:


[root@wx03 4]# perl t12.pl 
george
[root@wx03 4]# cat t12.pl 
@AoH = (
{
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{
husband => "george",
wife => "jane",
son => "elroy",
},
{
husband => "homer",
wife => "marge",
son => "bart",
},
);
print $AoH[1]->{husband};
print "\n";
[root@wx03 4]# perl t12.pl 
george



9.3.2 生成散列的数组:
[root@wx03 4]# cat t13.pl 
open (LOG ,"<","b.txt"); 
while (<LOG>) {
$rec = {};
for $field ( split ) {
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
push @AoH, $rec;
};

print @AoH;
print "\n";
use Data::Dumper;
$str=Dumper(@AoH);
print "\$str is $str\n";
[root@wx03 4]# perl t13.pl 
HASH(0x10b2160)HASH(0x10c6338)
$str is $VAR1 = {
          'band' => 'fred'
        };
$VAR2 = {
          'friend' => 'barney'
        };


9.4 散列的散列:

9.4.1  构成一个散列的散列:
[root@wx03 4]# cat t14.pl 
%HoH = (
flintstones => {
husband => "fred",
pal => "barney"
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy" # 键字需要引号
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart"
},
);
print %HoH;
print "\n";

print $HoH{simpsons}->{husband};
print "\n";
[root@wx03 4]# perl t14.pl 
jetsonsHASH(0x140a368)simpsonsHASH(0x140a4b8)flintstonesHASH(0x13f6160)
homer


9.4.2  生成散列的散列:



下面是一些填充一个散列的散列的技巧。要从一个下面格式的文件里读取数据:

[root@wx03 4]# cat t15.pl 
open (LOG ,"<","c.txt");
while( <LOG> ){
print "\$_ is $_\n";
$who = scan;
($key, $value) = split /=/, $_;
$HoH{$who}{$key} = $value;
};
print %HoH;
print "\n";
use Data::Dumper;
$str=Dumper(%HoH);
print "\$str is $str\n";
[root@wx03 4]# perl t15.pl 
$_ is husband=fred 

$_ is pal=barney 

$_ is wife=wilma

$_ is pet=dino

scanHASH(0x205c310)
$str is $VAR1 = 'scan';
$VAR2 = {
          'wife' => 'wilma
',
          'pet' => 'dino
',
          'pal' => 'barney 
',
          'husband' => 'fred 
'
        };


9.4.3 访问和打印散列的散列


9.5  函数的散列:

你还可以在你的数据结构里保存指向函数的引用, 就象你可以存储指向数组或者散列的引用
一样:


[root@wx03 4]# cat t16.pl 
%HoF = ( # Compose a hash of functions
exit => sub { exit },
help => \&show_help,
watch => sub { $watch = 1 },
mail => sub { return 2 },
edit => sub {print 3; },
delete => \&confirm_kill,
);
print &{$HoF{mail}};
[root@wx03 4]# perl t16.pl 
2[root@wx03 4]# 


9.6.1 更灵活的记录的组合,访问和打印;


posted @ 2016-05-27 13:57  czcb  阅读(103)  评论(0编辑  收藏  举报