第九章 關(guān)聯(lián)數(shù)組/哈希表
一、數(shù)組變量的限制1 : #!/usr/local/bin/perl運行結(jié)果如下:
2 :
3 : while ($inputline = <STDIN>) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : for ($count = 1; $count <= @wordlist;
8 : $count++) {
9 : $found = 0;
10: if ($wordlist[$count-1] eq $word) {
11: $found = 1;
12: $wordcount[$count-1] += 1;
13: last;
14: }
15: }
16: if ($found == 0) {
17: $oldlength = @wordlist;
18: $wordlist[$oldlength] = $word;
19: $wordcount[$oldlength] = 1;
20: }
21: }
22: }
23: print ("Capitalized words and number of occurrences:\n");
24: for ($count = 1; $count <= @wordlist; $count++) {
25: print ("$wordlist[$count-1]: $wordcount[$count-1]\n");
26: }
Here is a line of Input.這個程序每次從標(biāo)準(zhǔn)輸入文件讀一行文字,第四行起的循環(huán)匹配每行中首字母大寫的單詞,每找到一個循環(huán)一次,賦給簡單變量$word。在第六行中去掉標(biāo)點后,查看該單詞是否曾出現(xiàn)過,7~15行中在@wordlist中挨個元素做此檢查,如果某個元素與$word相等,@wordcount中相應(yīng)的元素就增加一個數(shù)。如果沒有出現(xiàn)過,即@wordlist中沒有元素與$word相等,16~20行給@wordlist和@wordcount增加一個新元素。
This Input contains some Capitalized words.
^D
Capitalized words and number of occurrences:
Here: 1
Input: 2
This: 1
Capitalized: 1
$fruit{"bananas"}簡單變量也可作為下標(biāo),如:
$number{3.14159}
$integer{-7}
1 : #!/usr/local/bin/perl運行結(jié)果如下:
2 :
3 : while ($inputline =) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : $wordlist{$word} += 1;
8 : }
9 : }
10: print ("Capitalized words and number of occurrences:\n");
11: foreach $capword (keys(%wordlist)) {
12: print ("$capword: $wordlist{$capword}\n");
13: }
Here is a line of Input.你可以看到,這次程序簡單多了,讀取輸入并存貯各單詞數(shù)目從20行減少到了7行。
This Input contains some Capitalized words.
^D
Capitalized words and number of occurrences:
This: 1
Input: 2
Here: 1
Capitalized: 1
foreach $capword (sort keys(%wordlist)) {五、創(chuàng)建關(guān)聯(lián)數(shù)組
print ("$capword: $wordlist{$capword}\n");
}
注:用列表給關(guān)聯(lián)數(shù)組賦值時,Perl5允許使用"=>"或","來分隔下標(biāo)與值,用"=>"可讀性更好些,上面語句等效于:下標(biāo)為apples的元素,值為17 下標(biāo)為bananas的元素,值為9 下標(biāo)為oranges的元素,值為none
1: #!/usr/local/bin/perl運行結(jié)果如下:
2:
3: $inputline = <STDIN>;
4: $inputline =~ s/^\s+|\s+\n$//g;
5: %fruit = split(/\s+/, $inputline);
6: print ("Number of bananas: $fruit{\"bananas\"}\n");
oranges 5 apples 7 bananas 11 cherries 6七、元素的增刪
Number of bananas: 11
1、一定要使用delete函數(shù)來刪除關(guān)聯(lián)數(shù)組的元素,這是唯一的方法。八、列出數(shù)組的索引和值
2、一定不要對關(guān)聯(lián)數(shù)組使用內(nèi)嵌函數(shù)push、pop、shift及splice,因為其元素位置是隨機(jī)的。
這里,@fruitsubs被賦給apples、bananas、cherries構(gòu)成的列表,再次提請注意,此列表沒有次序,若想按字母順序排列,可使用sort()函數(shù)。%fruit = ("apples", 9, "bananas", 23, "cherries", 11); @fruitsubs = keys(%fruits);
這里,@fruitvalues可能的結(jié)果為(9,23.11),次序可能不同。%fruit = ("apples", 9, "bananas", 23, "cherries", 11); @fruitvalues = values(%fruits);
foreach $holder (keys(%records)){Perl提供一種更有效的循環(huán)方式,使用內(nèi)嵌函數(shù)each(),如:
$record = $records{$holder};
}
%records = ("Maris", 61, "Aaron", 755, "Young", 511);each()函數(shù)每次返回一個雙元素的列表,其第一個元素為下標(biāo),第二個元素為相應(yīng)的值,最后返回一個空列表。
while (($holder, $record) = each(%records)) {
# stuff goes here
}
上例中,簡單變量$header含有鏈表中第一個單詞,它同時也是關(guān)聯(lián)數(shù)組第一個元素的下標(biāo),其值baker又是下一個元素的下標(biāo),依此類推。%words = ("abel", "baker", "baker", "charlie", "charlie", "delta", "delta", ""); $header = "abel";
1 : #!/usr/local/bin/perl運行結(jié)果如下:
2 :
3 : # initialize list to empty
4 : $header = "";
5 : while ($line = <STDIN>) {
6 : # remove leading and trailing spaces
7 : $line =~ s/^\s+|\s+$//g;
8 : @words = split(/\s+/, $line);
9 : foreach $word (@words) {
10: # remove closing punctuation, if any
11: $word =~ s/[.,;:-]$//;
12: # convert all words to lower case
13: $word =~ tr/A-Z/a-z/;
14: &add_word_to_list($word);
15: }
16: }
17: &print_list;
18:
19: sub add_word_to_list {
20: local($word) = @_;
21: local($pointer);
22:
23: # if list is empty, add first item
24: if ($header eq "") {
25: $header = $word;
26: $wordlist{$word} = "";
27: return;
28: }
29: # if word identical to first element in list,
30: # do nothing
31: return if ($header eq $word);
32: # see whether word should be the new
33: # first word in the list
34: if ($header gt $word) {
35: $wordlist{$word} = $header;
36: $header = $word;
37: return;
38: }
39: # find place where word belongs
40: $pointer = $header;
41: while ($wordlist{$pointer} ne "" &&
42: $wordlist{$pointer} lt $word) {
43: $pointer = $wordlist{$pointer};
44: }
45: # if word already seen, do nothing
46: return if ($word eq $wordlist{$pointer});
47: $wordlist{$word} = $wordlist{$pointer};
48: $wordlist{$pointer} = $word;
49: }
50:
51: sub print_list {
52: local ($pointer);
53: print ("Words in this file:\n");
54: $pointer = $header;
55: while ($pointer ne "") {
56: print ("$pointer\n");
57: $pointer = $wordlist{$pointer};
58: }
59: }
Here are some words.此程序分為三個部分:
Here are more words.
Here are still more words.
^D
Words in this file:
are
here
more
some
still
words
第3~17行為主程序,第4行初始化鏈表,將表頭變量$header設(shè)為空串,第5行起的循環(huán)每次讀取一行輸入,第7行去掉頭、尾的空格,第8行將句子分割成單詞。9~15行的內(nèi)循環(huán)每次處理一個單詞,如果該單詞的最后一個字符是標(biāo)點符號,就去掉。第13行把單詞轉(zhuǎn)換成全小寫形式,第14行傳遞給子程序add_word_to_list。主程序:讀取輸入并轉(zhuǎn)換到相應(yīng)的格式。 子程序:add_word_to_list,建立排序單詞鏈表。 子程序:print_list,輸出單詞鏈表
foreach $word (sort keys(%wordlist)) {但是,這里涉及的指針的概念在其它數(shù)據(jù)結(jié)構(gòu)中很有意義。
# print the sorted list, or whatever }
struce{我們要做的是定義一個含有三個元素的關(guān)聯(lián)數(shù)組,下標(biāo)分別為field1、field2、field3,如:
int field1;
int field2;
int field3; }mystructvar;
%mystructvar = ("field1" , "" ,像上面C語言的定義一樣,這個關(guān)聯(lián)數(shù)組%mystrctvar有三個元素,下標(biāo)分別為field1、field2、field3,各元素初始值均為空串。對各元素的訪問和賦值通過指定下標(biāo)來進(jìn)行,如:
"field2" , "" ,
"field3" , "" ,);
有多種使用關(guān)聯(lián)數(shù)組實現(xiàn)樹結(jié)構(gòu)的方法,最好的一種應(yīng)該是:給子節(jié)點分別加上left和right以訪問之。例如,alphaleft和alpharight指向alpha的左右子節(jié)點。下面是用此方法創(chuàng)建二叉樹并遍歷的例程:因為每個子節(jié)點均為一個樹,所以左/右子節(jié)點也稱為左/右子樹。(有時稱左/右分支) 第一個節(jié)點(不是任何節(jié)點的子節(jié)點的節(jié)點)稱為樹的根。 沒有孩子(子節(jié)點)的節(jié)點稱為葉節(jié)點。
1 : #!/usr/local/bin/perl結(jié)果輸出如下:
2 :
3 : $rootname = "parent";
4 : %tree = ("parentleft", "child1",
5 : "parentright", "child2",
6 : "child1left", "grandchild1",
7 : "child1right", "grandchild2",
8 : "child2left", "grandchild3",
9 : "child2right", "grandchild4");
10: # traverse tree, printing its elements
11: &print_tree($rootname);
12:
13: sub print_tree {
14: local ($nodename) = @_;
15: local ($leftchildname, $rightchildname);
16:
17: $leftchildname = $nodename . "left";
18: $rightchildname = $nodename . "right";
19: if ($tree{$leftchildname} ne "") {
20: &print_tree($tree{$leftchildname});
21: }
22: print ("$nodename\n");
23: if ($tree{$rightchildname} ne "") {
24: &print_tree($tree{$rightchildname});
25: }
26: }
grandchild1該程序創(chuàng)建的二叉樹如下圖:
child1
grandchild2
parent
grandchild3
child2
grandchild4