?? perl 語言-perl 中文教程(第九章).htm
字號:
<BLOCKQUOTE><PRE>%fruit = ("apples", 9,
"bananas", 23,
"cherries", 11);
@fruitsubs = keys(%fruits);
</PRE></BLOCKQUOTE>
<P> 這里,@fruitsubs被賦給apples、bananas、cherries構成的列表,再次提請注意,此列表沒有次序,若想按字母順序排列,可使用sort()函數。<BR> @fruitindexes
= sort
keys(%fruits);<BR> 這樣結果為("apples","bananas","cherries")。類似的,內嵌函數values()返回關聯數組值的列表,如:<BR></P>
<BLOCKQUOTE><PRE>%fruit = ("apples", 9,
"bananas", 23,
"cherries", 11);
@fruitvalues = values(%fruits);
</PRE></BLOCKQUOTE>
<P> 這里,@fruitvalues可能的結果為(9,23.11),次序可能不同。<BR><BR><A
name=9>九、用關聯數組循環</A><BR> 前面已經出現過利用keys()函數的foreach循環語句,這種循環效率比較低,因為每返回一個下標,還得再去尋找其值,如:<BR></P>
<BLOCKQUOTE>
<P>foreach $holder (keys(%records)){<BR> $record =
$records{$holder};<BR>} </P></BLOCKQUOTE>
<P> Perl提供一種更有效的循環方式,使用內嵌函數each(),如:<BR></P>
<BLOCKQUOTE>
<P>%records = ("Maris", 61, "Aaron", 755, "Young", 511);<BR>while
(($holder, $record) = each(%records)) {<BR> # stuff goes here<BR>}
</P></BLOCKQUOTE>
<P> each()函數每次返回一個雙元素的列表,其第一個元素為下標,第二個元素為相應的值,最后返回一個空列表。<BR> 注意:千萬不要在each()循環中添加或刪除元素,否則會產生不可預料的后果。<BR><BR><A
name=10>十、用關聯數組創建數據結構</A><BR> 用關聯數組可以模擬在其它高級語言中常見的多種數據結構,本節講述如何用之實現:鏈表、結構和樹。<BR><A
name=10.1></A>1、(單)鏈表<BR> 鏈表是一種比較簡單的數據結構,可以按一定的次序存貯值。每個元素含有兩個域,一個是值,一個是引用(或稱指針),指向鏈表中下一個元素。一個特殊的頭指針指向鏈表的第一個元素。<BR> 在Perl中,鏈表很容易用關聯數組實現,因為一個元素的值可以作為下一個元素的索引。下例為按字母順序排列的單詞鏈表:<BR></P>
<BLOCKQUOTE><PRE>%words = ("abel", "baker",
"baker", "charlie",
"charlie", "delta",
"delta", "");
$header = "abel";
</PRE></BLOCKQUOTE>
<P> 上例中,簡單變量$header含有鏈表中第一個單詞,它同時也是關聯數組第一個元素的下標,其值baker又是下一個元素的下標,依此類推。<BR> 下標為delta的最后一個元素的值為空串,表示鏈表的結束。<BR> 在將要處理的數據個數未知或其隨程序運行而增長的情況下,鏈表十分有用。下例用鏈表按字母次序輸出一個文件中的單詞。<BR></P>
<BLOCKQUOTE>
<P>1 : #!/usr/local/bin/perl<BR>2 :<BR>3 : # initialize list to
empty<BR>4 : $header = "";<BR>5 : while ($line = <STDIN>) {<BR>6 :
# remove leading and trailing spaces<BR>7 : $line =~
s/^\s+|\s+$//g;<BR>8 : @words = split(/\s+/, $line);<BR>9 :
foreach $word (@words) {<BR>10: # remove closing
punctuation, if any<BR>11: $word =~ s/[.,;:-]$//;<BR>12:
# convert all words to lower case<BR>13:
$word =~ tr/A-Z/a-z/;<BR>14:
&add_word_to_list($word);<BR>15: }<BR>16: }<BR>17:
&print_list;<BR>18:<BR>19: sub add_word_to_list {<BR>20:
local($word) = @_;<BR>21: local($pointer);<BR>22:<BR>23: #
if list is empty, add first item<BR>24: if ($header eq "")
{<BR>25: $header = $word;<BR>26:
$wordlist{$word} = "";<BR>27: return;<BR>28:
}<BR>29: # if word identical to first element in list,<BR>30:
# do nothing<BR>31: return if ($header eq $word);<BR>32:
# see whether word should be the new<BR>33: # first word
in the list<BR>34: if ($header gt $word) {<BR>35:
$wordlist{$word} = $header;<BR>36: $header = $word;<BR>37:
return;<BR>38: }<BR>39: # find place where
word belongs<BR>40: $pointer = $header;<BR>41: while
($wordlist{$pointer} ne "" &&<BR>42:
$wordlist{$pointer} lt $word) {<BR>43: $pointer =
$wordlist{$pointer};<BR>44: }<BR>45: # if word already
seen, do nothing<BR>46: return if ($word eq
$wordlist{$pointer});<BR>47: $wordlist{$word} =
$wordlist{$pointer};<BR>48: $wordlist{$pointer} = $word;<BR>49:
}<BR>50:<BR>51: sub print_list {<BR>52: local ($pointer);<BR>53:
print ("Words in this file:\n");<BR>54: $pointer =
$header;<BR>55: while ($pointer ne "") {<BR>56:
print ("$pointer\n");<BR>57: $pointer =
$wordlist{$pointer};<BR>58: }<BR>59: } </P></BLOCKQUOTE>
<P> 運行結果如下:<BR></P>
<BLOCKQUOTE>
<P>Here are some words.<BR>Here are more words.<BR>Here are still more
words.<BR>^D<BR>Words in this
file:<BR>are<BR>here<BR>more<BR>some<BR>still<BR>words </P></BLOCKQUOTE>
<P> 此程序分為三個部分:<BR></P>
<BLOCKQUOTE>
<UL>
<LI>主程序:讀取輸入并轉換到相應的格式。
<LI>子程序:add_word_to_list,建立排序單詞鏈表。
<LI>子程序:print_list,輸出單詞鏈表 </LI></UL></BLOCKQUOTE>
<P> 第3~17行為主程序,第4行初始化鏈表,將表頭變量$header設為空串,第5行起的循環每次讀取一行輸入,第7行去掉頭、尾的空格,第8行將句子分割成單詞。9~15行的內循環每次處理一個單詞,如果該單詞的最后一個字符是標點符號,就去掉。第13行把單詞轉換成全小寫形式,第14行傳遞給子程序add_word_to_list。<BR> 子程序add_word_to_list先在第24行處檢查鏈表是否為空。如果是,第25行將單詞賦給$header,26行創建鏈表第一個元素,存貯在關聯數組%wordlist中。如果鏈表非空,37行檢查第一個元素是否與該單詞相同,如果相同,就立刻返回。下一步檢查這一新單詞是否應該為鏈表第一個元素,即其按字母順序先于$header。如果是這樣,則:<BR> 1、創建一個新元素,下標為該新單詞,其值為原第一個單詞。<BR> 2、該新單詞賦給$header。<BR> 如果該新單詞不該為第一個元素,則40~44行利用局域變量$pointer尋找其合適的有效位置,41~44行循環到$wordlist{$pointer}大于或等于$word為止。接下來46行查看該單詞是否已在鏈表中,如果在就返回,否則47~48行將其添加到鏈表中。首先47行創建新元素$wordlist{$word},其值為$wordlist{$pointer},這時$wordlist{$word}和$wordlist{$pointer}指向同一個單詞。然后,48行將$wordlist{$pointer}的值賦為$word,即將$wordlist{$pointer}指向剛創建的新元素$wordlist{$word}。<BR> 最后當處理完畢后,子程序print_list()依次輸出鏈表,局域變量$pointer含有正在輸出的值,$wordlist{$pointer}為下一個要輸出的值。<BR> 注:一般不需要用鏈表來做這些工作,用sort()和keys()在關聯數組中循環就足夠了,如:<BR></P>
<BLOCKQUOTE>
<P>foreach $word (sort keys(%wordlist)) {<BR> # print the sorted
list, or whatever } </P></BLOCKQUOTE>
<P> 但是,這里涉及的指針的概念在其它數據結構中很有意義。<BR><A
name=10.2>2、結構</A><BR> 許多編程語言可以定義結構(structure),即一組數據的集合。結構中的每個元素有其自己的名字,并通過該名字來訪問。<BR> Perl不直接提供結構這種數據結構,但可以用關聯數組來模擬。例如模擬C語言中如下的結構:<BR></P>
<BLOCKQUOTE>
<P>struce{<BR> int field1;<BR> int field2;<BR> int
field3; }mystructvar; </P></BLOCKQUOTE>
<P> 我們要做的是定義一個含有三個元素的關聯數組,下標分別為field1、field2、field3,如:<BR></P>
<BLOCKQUOTE>
<P>%mystructvar = ("field1" , "" ,<BR> "field2" , ""
,<BR> "field3" , "" ,); </P></BLOCKQUOTE>
<P> 像上面C語言的定義一樣,這個關聯數組%mystrctvar有三個元素,下標分別為field1、field2、field3,各元素初始值均為空串。對各元素的訪問和賦值通過指定下標來進行,如:<BR> $mystructvar{"field1"}
= 17;<BR><A
name=10.3>3、樹</A><BR> 另一個經常使用的數據結構是樹。樹與鏈表類似,但每個節點指向的元素多于一個。最簡單的樹是二叉樹,每個節點指向另外兩個元素,稱為左子節點和右子節點(或稱孩子),每個子節點又指向兩個孫子節點,依此類推。<BR> 注:此處所說的樹像上述鏈表一樣是單向的,每個節點指向其子節點,但子節點并不指向父節點。<BR> 樹的概念可以如下描述:<BR></P>
<BLOCKQUOTE>
<UL>
<LI>因為每個子節點均為一個樹,所以左/右子節點也稱為左/右子樹。(有時稱左/右分支)
<LI>第一個節點(不是任何節點的子節點的節點)稱為樹的根。
<LI>沒有孩子(子節點)的節點稱為葉節點。 </LI></UL></BLOCKQUOTE>
<P> 有多種使用關聯數組實現樹結構的方法,最好的一種應該是:給子節點分別加上left和right以訪問之。例如,alphaleft和alpharight指向alpha的左右子節點。下面是用此方法創建二叉樹并遍歷的例程:<BR></P>
<BLOCKQUOTE>
<P>1 : #!/usr/local/bin/perl<BR>2 :<BR>3 : $rootname = "parent";<BR>4 :
%tree = ("parentleft", "child1",<BR>5 :
"parentright", "child2",<BR>6 :
"child1left", "grandchild1",<BR>7 :
"child1right", "grandchild2",<BR>8 :
"child2left", "grandchild3",<BR>9 :
"child2right", "grandchild4");<BR>10: # traverse tree, printing its
elements<BR>11: &print_tree($rootname);<BR>12:<BR>13: sub print_tree
{<BR>14: local ($nodename) = @_;<BR>15: local
($leftchildname, $rightchildname);<BR>16:<BR>17: $leftchildname =
$nodename . "left";<BR>18: $rightchildname = $nodename .
"right";<BR>19: if ($tree{$leftchildname} ne "") {<BR>20:
&print_tree($tree{$leftchildname});<BR>21: }<BR>22:
print ("$nodename\n");<BR>23: if ($tree{$rightchildname}
ne "") {<BR>24:
&print_tree($tree{$rightchildname});<BR>25: }<BR>26: }
</P></BLOCKQUOTE>
<P> 結果輸出如下:<BR></P>
<BLOCKQUOTE>
<P>grandchild1<BR>child1<BR>grandchild2<BR>parent<BR>grandchild3<BR>child2<BR>grandchild4
</P></BLOCKQUOTE>
<P> 該程序創建的二叉樹如下圖:<BR></P>
<P align=center><IMG height=126
src="Perl 語言-Perl 中文教程(第九章).files/tree.gif" width=253></P>
<P> 注意函數print_tree()以次序“左子樹、節點、右子樹”來輸出各節點的名字,這種遍歷次序稱為“左序遍歷”。如果把第22行移到19行前,先輸出節點明,再輸出左子樹、右子樹,則為“中序遍歷”,如果把第22行移到25行后,輸出次序為左子樹、右子樹、節點,則為“右序遍歷”。<BR> 可以用同樣的方法,即連接字符串構成下標,來創建其它的數據結構,如數據庫等。</P></DIV></DIV></TD></TR></TBODY></TABLE>
<DIV align=center></DIV>
<DIV align=center><BR></DIV>
<DIV align=center><SPAN class=myFont><A
href="http://www.sun126.com/perl5/perl5-8.htm">上頁</A> <A
href="http://www.sun126.com/perl5/perl5-10.htm">下頁</A> <A
href="http://www.sun126.com/perl5/perl5index.htm">回目錄</A> <A
href="http://www.sun126.com/perl5/perl5-9.htm#a"><FONT face="Arial, 宋體">Go
Top</FONT></A></SPAN><BR><BR></DIV>
<TABLE height=50 cellSpacing=0 cellPadding=0 width="100%" bgColor=#000000
border=0>
<TBODY>
<TR>
<TD bgColor=#cccc99 height=4>
<DIV align=center><IMG height=4 src="" width=4></DIV></TD></TR>
<TR>
<TD height=50>
<DIV align=center><FONT class=myfont size=2><SPAN class=myfont><FONT
color=#99cc99><A href="http://www.sun126.com/bbs/ccb/index.cgi"><FONT
color=#99cc99>中國CCB論壇</FONT></A> 整理 麻辣
2003.7.10</FONT></SPAN></FONT><FONT class=myfont color=#99cc99
size=2><SPAN class=myfont><FONT
color=#99cc66><BR></FONT></SPAN></FONT><SPAN class=myfont><FONT
class=myfont><SPAN class=myfont><FONT face="Arial, Helvetica, sans-serif"
color=#99cc99>© 2000
http://www.sun126.com</FONT></SPAN></FONT></SPAN></DIV></TD></TR></TBODY></TABLE></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -