Perl 免費提供許多資料結構,這些資料結構在其他程式設計語言裡是需要你自己制作的。
比如那些計算機 科學的新芽們都需要學習的堆棧和隊列在 Perl 裡都隻是數組
pop:
pop 操作将數組的最後一個元素取出并傳回:
Vsftp:/root/perl/5# cat a1.pl
my @arr=qw/a b c d e f g/;
print "\@arr is @arr\n";
pop @arr;
print "\@arr is @arr\n";
Vsftp:/root/perl/5# perl a1.pl
@arr is a b c d e f g
@arr is a b c d e f
push,它可以将一個元素(或者一列元素)加在數組的末尾:
Vsftp:/root/perl/5# cat a1.pl
my @arr=qw/a b c d e f g/;
print "\@arr is @arr\n";
pop @arr;
print "\@arr is @arr\n";
push (@arr,'xxyy');
print "\@arr is @arr\n";
Vsftp:/root/perl/5# perl a1.pl
@arr is a b c d e f g
@arr is a b c d e f
@arr is a b c d e f xxyy
shift 拿掉數組最左邊的一個值
Vsftp:/root/perl/5# cat a2.pl
my @arr=qw/a b c d e f g/;
print "\@arr is @arr\n";
shift @arr;
print "\@arr is @arr\n";
Vsftp:/root/perl/5# perl a2.pl
@arr is a b c d e f g
@arr is b c d e f g
unshift:添加一個值到數組的最左邊。
Vsftp:/root/perl/5# cat a2.pl
my @arr=qw/a b c d e f g/;
print "\@arr is @arr\n";
unshift (@arr,'999');
print "\@arr is @arr\n";
Vsftp:/root/perl/5# perl a2.pl
@arr is a b c d e f g
@arr is 999 a b c d e f g
9.1 數組的數組
9.1.1 建立和通路一個兩維數組
Vsftp:/root/perl/5# cat a3.pl
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);
print @Aoa;
print "\n";
Vsftp:/root/perl/5# perl a3.pl
ARRAY(0xb55d48)ARRAY(0xb71358)ARRAY(0xb71538)
Vsftp:/root/perl/5# cat a3.pl
@Aoa = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);
print @{$Aoa[0]};
print "\n";
print $Aoa[0]->[1];
print "\n";
Vsftp:/root/perl/5# perl a3.pl
fredbarney
barney
# 建立一個指向一個數組的數組的引用。
Vsftp:/root/perl/5# cat a4.pl
$ref_to_AoA = [
[ "fred", "barney", "pebbles", "bamm bamm", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "elroy", "judy", ],
];
print $ref_to_AoA;
print "\n";
print @{$ref_to_AoA};
print "\n";
print $ref_to_AoA->[0]->[3];
print "\n";
Vsftp:/root/perl/5# perl a4.pl
ARRAY(0x104c638)
ARRAY(0x1029d48)ARRAY(0x10455e0)ARRAY(0x1045670)
bamm bamm
請記住在每一對相鄰的花括弧或方括弧之間有一個隐含的 ->
9.1.2 自行生長
Vsftp:/root/perl/5# cat aa
1 4 7
a1 a4 a7
z1 a6 z9
Vsftp:/root/perl/5# cat a5.pl
open (A,"aa");
while (<A>){
chomp $_;
my @tmp = split(/\s+/,$_);
push @AOA ,[@tmp];
};
print @AOA;
print "\n";
print $AOA[1]->[1];
print "\n";
Vsftp:/root/perl/5# perl a5.pl
ARRAY(0xc02d48)ARRAY(0xc1e340)ARRAY(0xc1e5e0)
a4
/*****************************
Vsftp:/root/perl/5# perl a6.pl
Type of arg 1 to push must be array (not scalar dereference) at a6.pl line 6, near "];"
Execution of a6.pl aborted due to compilation errors.
Vsftp:/root/perl/5# cat a6.pl
open (A,"aa");
my @ref_to_AOA=\@AOA;
while (<A>){
chomp $_;
my @tmp = split(/\s+/,$_);
push $ref_to_AOA ,[@tmp];
};
print @AOA;
print "\n";
print $AOA[1]->[1];
print "\n";
Vsftp:/root/perl/5# perl a6.pl
Type of arg 1 to push must be array (not scalar dereference) at a6.pl line 6, near "];"
Execution of a6.pl aborted due to compilation errors.
Vsftp:/root/perl/5# cat a7.pl
sub func() {
my $a=shift;
my $b=shift;
return $a + $b;
};
for $x (0..9){ # 對每一行...
for $y (0..9) { # 對每一列...
$AoA[$x][$y] = &func($x, $y); # ...設定調用
}
};
print @AoA;
print "\n";
print @{$AoA[0]};
print "\n";
print @{$AoA[1]};
print "\n";
print @{$AoA[2]};
print "\n";
Vsftp:/root/perl/5# perl a7.pl
ARRAY(0x692358)ARRAY(0x69e290)ARRAY(0x69e3b0)ARRAY(0x69e4d0)ARRAY(0x69e5f0)ARRAY(0x69e710)ARRAY(0x69e830)ARRAY(0x69e950)ARRAY(0x69ea70)ARRAY(0x69eb90)
0123456789
12345678910
234567891011
這種方式的通路:
$AoA[$x][$y]
$AoA[$x]->[$y]
Vsftp:/root/perl/5# cat a8.pl
my @AoA=(1,2,3,[a1,a2,a3]);
print $AoA[3][2];
print "\n";
print $AoA[3]->[2];
print "\n";
Vsftp:/root/perl/5#
Vsftp:/root/perl/5# perl a8.pl
a3
a3
Vsftp:/root/perl/5# cat a9.pl
use Data::Dumper;
my $ref_to_AoA=[(1,f2,f3),(a1,a2,a3),(z1,z2,z3)];
print Dumper($ref_to_AoA);
print "\n";
print $ref_to_AoA->[0]."\n";
print $ref_to_AoA->[1]."\n";
print $ref_to_AoA->[2]."\n";
print $ref_to_AoA->[3]."\n";
print $ref_to_AoA->[4]."\n";
print $ref_to_AoA->[5]."\n";
print $ref_to_AoA->[6]."\n";
Vsftp:/root/perl/5# perl a9.pl
$VAR1 = [
1,
'f2',
'f3',
'a1',
'a2',
'a3',
'z1',
'z2',
'z3'
];
1
f2
f3
a1
a2
a3
z1
Vsftp:/root/perl/5# cat a10.pl
use Data::Dumper;
my $ref_to_AoA=[[1,f2,f3],[a1,a2,a3],[z1,z2,z3]];
print Dumper($ref_to_AoA);
print "\n";
print $ref_to_AoA->[0]."\n";
print $ref_to_AoA->[1]."\n";
print $ref_to_AoA->[2]."\n";
print $ref_to_AoA->[0]->[2]."\n";
Vsftp:/root/perl/5# perl a10.pl
$VAR1 = [
[
1,
'f2',
'f3'
],
[
'a1',
'a2',
'a3'
],
[
'z1',
'z2',
'z3'
]
];
ARRAY(0x1742d48)
ARRAY(0x1796628)
ARRAY(0x178aa00)
f3
/****************************
Vsftp:/root/perl/5# cat a11.pl
use Data::Dumper;
my $ref_to_AoA=[[1,f2,f3],[a1,a2,a3],[z1,z2,z3],[z1,z2,dsd3],[z1,z2,zdas3],[z1,z2,zda3]];
print Dumper($ref_to_AoA);
print "\n";
print $ref_to_AoA->[0]->[2]."\n";
print $ref_to_AoA->[1]->[2]."\n";
print $ref_to_AoA->[2]->[2]."\n";
print $ref_to_AoA->[3]->[2]."\n";
print $ref_to_AoA->[4]->[2]."\n";
print $ref_to_AoA->[5]->[2]."\n";
for $x (0..5) { # 對每一行...
$ref_to_AoA->[$x][2] = &func($x); # ...設定第四行
};
print $ref_to_AoA->[0]->[2]."\n";
print $ref_to_AoA->[1]->[2]."\n";
print $ref_to_AoA->[2]->[2]."\n";
print $ref_to_AoA->[3]->[2]."\n";
print $ref_to_AoA->[4]->[2]."\n";
print $ref_to_AoA->[5]->[2]."\n";
sub func() {
my $a=shift;
my $b=shift;
return $a + $b;
};
Vsftp:/root/perl/5# perl a11.pl
f3
a3
z3
dsd3
zdas3
zda3
0
1
2
3
4
5
Vsftp:/root/perl/5# cat a12.pl
use Data::Dumper;
my @AoA=([1,f2,f3],[a1,a2,a3],[z1,z2,z3],[z1,z2,dsd3],[z1,z2,zdas3],[z1,z2,zda3]);
print @{ $AoA[0] };
print "\n";
push @{ $AoA[0] }, "wilma", "betty";
print @{ $AoA[0] };
print "\n";
Vsftp:/root/perl/5# perl a12.pl
1f2f3
1f2f3wilmabetty
因為給 push 的參數必須是一個真正的數組,而不隻是一個指向 一個數組的引
用。是以,第一個參數絕對必須以 @ 字元開頭。而跟在 @ 後面的東西則可以忽略一些。
9.1.3 通路的和列印
現在把資料結構列印出來,如果你隻想要一個元素,下面的就足夠了:
Vsftp:/root/perl/5# cat a13.pl
my @AoA=(1,2,3,[a1,a2,a3,a4]);
print $AoA[3][2];
print "\n";
Vsftp:/root/perl/5# perl a13.pl
a3
Vsftp:/root/perl/5# cat a13.pl
my @AoA=(1,2,3,[a1,a2,a3,a4]);
print @AoA;
print "\n";
Vsftp:/root/perl/5# perl a13.pl
123ARRAY(0x2161d48)
Vsftp:/root/perl/5# cat a13.pl
my @AoA=([1],[2],[3],[a1,a2,a3,a4]);
print @AoA;
print "\n";
for $row (@AoA) {
print "@$row\n";
}
Vsftp:/root/perl/5# perl a13.pl
ARRAY(0x144fd48)ARRAY(0x146b340)ARRAY(0x146b550)ARRAY(0x146b598)
1
2
3
a1 a2 a3 a4
Vsftp:/root/perl/5# cat a13.pl
my @AoA=([1],[2],[3],[a1,a2,a3,a4]);
for $i (0..$#AoA) {
print "row $i is: @{$AoA[$i]}\n";
}
Vsftp:/root/perl/5# vim a13.pl
Vsftp:/root/perl/5# cat a13.pl
my @AoA=([1],[2],[3],[a1,a2,a3,a4]);
for $i (0..$#AoA) {
print "row $i is: @{$AoA[$i]}\n";
}
Vsftp:/root/perl/5# perl a13.pl
row 0 is: 1
row 1 is: 2
row 2 is: 3
row 3 is: a1 a2 a3 a4
9.1.4 片段
取出片段:
Vsftp:/root/perl/5# cat a14.pl
my @AoA=([1,2,3],['aA','ADS','DSD'],['DAD',323,233],[232,'FF','344G'],['sas21','121','sad1231','asda1231','3131','sad213','adda','ewds']);
print @AoA;
print "\n";
print $AoA[4]->[2];
print "\n";
@part = ();
for ($y =3 ; $y < 8; $y++) {
push @part, $AoA[4][$y];
}
print @part;
print "\n";
Vsftp:/root/perl/5# perl a14.pl
ARRAY(0x900d48)ARRAY(0x927638)ARRAY(0x9276b0)ARRAY(0x927728)ARRAY(0x9277a0)
sad1231
asda12313131sad213addaewds
Vsftp:/root/perl/5#
9.1.5 常見錯誤:
Vsftp:/root/perl/5# perl a16.pl
ARRAY(0x1933d48) ARRAY(0x194f358) ARRAY(0x194f538)
Vsftp:/root/perl/5# cat a16.pl
@AoA = ([2, 3], [4, 5, 7], [0] );
print "@AoA";
print "\n";
Vsftp:/root/perl/5# perl a16.pl
ARRAY(0x9f4d48) ARRAY(0xa10358) ARRAY(0xa10538)
Vsftp:/root/perl/5# cat a17.pl
sub somefunc() {
my $a=shift;
return $a=3;
};
for $i (1..10) {
@array = &somefunc($i);
$AoA[$i] = [@array]; # 把數組引用給到對應的數組元素
};
print @AoA;
print "\n";
Vsftp:/root/perl/5# perl a17.pl
ARRAY(0x2547f40)ARRAY(0x2563358)ARRAY(0x2563628)ARRAY(0x2563670)ARRAY(0x25636b8)ARRAY(0x256c038)ARRAY(0x256c080)ARRAY(0x256c0c8)ARRAY(0x256c110)ARRAY(0x256c158)