⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perl 语言-perl 中文教程(第九章).htm

📁 perl的中文教程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
      <BLOCKQUOTE><PRE>%fruit = ("apples", 9,
          "bananas", 23,
          "cherries", 11);
@fruitsubs = keys(%fruits);
</PRE></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;这里,@fruitsubs被赋给apples、bananas、cherries构成的列表,再次提请注意,此列表没有次序,若想按字母顺序排列,可使用sort()函数。<BR>&nbsp;&nbsp;&nbsp;&nbsp;@fruitindexes 
      = sort 
      keys(%fruits);<BR>&nbsp;&nbsp;&nbsp;&nbsp;这样结果为("apples","bananas","cherries")。类似的,内嵌函数values()返回关联数组值的列表,如:<BR></P>
      <BLOCKQUOTE><PRE>%fruit = ("apples", 9,
           "bananas", 23,
           "cherries", 11);
@fruitvalues = values(%fruits);
</PRE></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;这里,@fruitvalues可能的结果为(9,23.11),次序可能不同。<BR><BR><A 
      name=9>九、用关联数组循环</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;前面已经出现过利用keys()函数的foreach循环语句,这种循环效率比较低,因为每返回一个下标,还得再去寻找其值,如:<BR></P>
      <BLOCKQUOTE>
        <P>foreach $holder (keys(%records)){<BR>&nbsp; $record = 
        $records{$holder};<BR>} </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;Perl提供一种更有效的循环方式,使用内嵌函数each(),如:<BR></P>
      <BLOCKQUOTE>
        <P>%records = ("Maris", 61, "Aaron", 755, "Young", 511);<BR>while 
        (($holder, $record) = each(%records)) {<BR>&nbsp; # stuff goes here<BR>} 
        </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;each()函数每次返回一个双元素的列表,其第一个元素为下标,第二个元素为相应的值,最后返回一个空列表。<BR>&nbsp;&nbsp;&nbsp;&nbsp;注意:千万不要在each()循环中添加或删除元素,否则会产生不可预料的后果。<BR><BR><A 
      name=10>十、用关联数组创建数据结构</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;用关联数组可以模拟在其它高级语言中常见的多种数据结构,本节讲述如何用之实现:链表、结构和树。<BR><A 
      name=10.1></A>1、(单)链表<BR>&nbsp;&nbsp;&nbsp;&nbsp;链表是一种比较简单的数据结构,可以按一定的次序存贮值。每个元素含有两个域,一个是值,一个是引用(或称指针),指向链表中下一个元素。一个特殊的头指针指向链表的第一个元素。<BR>&nbsp;&nbsp;&nbsp;&nbsp;在Perl中,链表很容易用关联数组实现,因为一个元素的值可以作为下一个元素的索引。下例为按字母顺序排列的单词链表:<BR></P>
      <BLOCKQUOTE><PRE>%words = ("abel", "baker", 
          "baker", "charlie",
          "charlie", "delta",
          "delta", "");
$header = "abel";
</PRE></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;上例中,简单变量$header含有链表中第一个单词,它同时也是关联数组第一个元素的下标,其值baker又是下一个元素的下标,依此类推。<BR>&nbsp;&nbsp;&nbsp;&nbsp;下标为delta的最后一个元素的值为空串,表示链表的结束。<BR>&nbsp;&nbsp;&nbsp;&nbsp;在将要处理的数据个数未知或其随程序运行而增长的情况下,链表十分有用。下例用链表按字母次序输出一个文件中的单词。<BR></P>
      <BLOCKQUOTE>
        <P>1 : #!/usr/local/bin/perl<BR>2 :<BR>3 : # initialize list to 
        empty<BR>4 : $header = "";<BR>5 : while ($line = &lt;STDIN&gt;) {<BR>6 : 
        &nbsp; # remove leading and trailing spaces<BR>7 : &nbsp; $line =~ 
        s/^\s+|\s+$//g;<BR>8 : &nbsp; @words = split(/\s+/, $line);<BR>9 : 
        &nbsp; foreach $word (@words) {<BR>10: &nbsp; &nbsp; # remove closing 
        punctuation, if any<BR>11: &nbsp; &nbsp; $word =~ s/[.,;:-]$//;<BR>12: 
        &nbsp; &nbsp; # convert all words to lower case<BR>13: &nbsp; &nbsp; 
        $word =~ tr/A-Z/a-z/;<BR>14: &nbsp; &nbsp; 
        &amp;add_word_to_list($word);<BR>15: &nbsp; }<BR>16: }<BR>17: 
        &amp;print_list;<BR>18:<BR>19: sub add_word_to_list {<BR>20: &nbsp; 
        local($word) = @_;<BR>21: &nbsp; local($pointer);<BR>22:<BR>23: &nbsp; # 
        if list is empty, add first item<BR>24: &nbsp; if ($header eq "") 
        {<BR>25: &nbsp; &nbsp; $header = $word;<BR>26: &nbsp; &nbsp; 
        $wordlist{$word} = "";<BR>27: &nbsp; &nbsp; return;<BR>28: &nbsp; 
        }<BR>29: &nbsp; # if word identical to first element in list,<BR>30: 
        &nbsp; # do nothing<BR>31: &nbsp; return if ($header eq $word);<BR>32: 
        &nbsp; # see whether word should be the new<BR>33: &nbsp; # first word 
        in the list<BR>34: &nbsp; if ($header gt $word) {<BR>35: &nbsp; &nbsp; 
        $wordlist{$word} = $header;<BR>36: &nbsp; &nbsp; $header = $word;<BR>37: 
        &nbsp; &nbsp; return;<BR>38: &nbsp; }<BR>39: &nbsp; # find place where 
        word belongs<BR>40: &nbsp; $pointer = $header;<BR>41: &nbsp; while 
        ($wordlist{$pointer} ne "" &amp;&amp;<BR>42: &nbsp; &nbsp; 
        $wordlist{$pointer} lt $word) {<BR>43: &nbsp; &nbsp; $pointer = 
        $wordlist{$pointer};<BR>44: &nbsp; }<BR>45: &nbsp; # if word already 
        seen, do nothing<BR>46: &nbsp; return if ($word eq 
        $wordlist{$pointer});<BR>47: &nbsp; $wordlist{$word} = 
        $wordlist{$pointer};<BR>48: &nbsp; $wordlist{$pointer} = $word;<BR>49: 
        }<BR>50:<BR>51: sub print_list {<BR>52: &nbsp; local ($pointer);<BR>53: 
        &nbsp; print ("Words in this file:\n");<BR>54: &nbsp; $pointer = 
        $header;<BR>55: &nbsp; while ($pointer ne "") {<BR>56: &nbsp; &nbsp; 
        print ("$pointer\n");<BR>57: &nbsp; &nbsp; $pointer = 
        $wordlist{$pointer};<BR>58: &nbsp; }<BR>59: } </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;运行结果如下:<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>&nbsp;&nbsp;&nbsp;&nbsp;此程序分为三个部分:<BR></P>
      <BLOCKQUOTE>
        <UL>
          <LI>主程序:读取输入并转换到相应的格式。 
          <LI>子程序:add_word_to_list,建立排序单词链表。 
          <LI>子程序:print_list,输出单词链表 </LI></UL></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;第3~17行为主程序,第4行初始化链表,将表头变量$header设为空串,第5行起的循环每次读取一行输入,第7行去掉头、尾的空格,第8行将句子分割成单词。9~15行的内循环每次处理一个单词,如果该单词的最后一个字符是标点符号,就去掉。第13行把单词转换成全小写形式,第14行传递给子程序add_word_to_list。<BR>&nbsp;&nbsp;&nbsp;&nbsp;子程序add_word_to_list先在第24行处检查链表是否为空。如果是,第25行将单词赋给$header,26行创建链表第一个元素,存贮在关联数组%wordlist中。如果链表非空,37行检查第一个元素是否与该单词相同,如果相同,就立刻返回。下一步检查这一新单词是否应该为链表第一个元素,即其按字母顺序先于$header。如果是这样,则:<BR>&nbsp;&nbsp;&nbsp;&nbsp;1、创建一个新元素,下标为该新单词,其值为原第一个单词。<BR>&nbsp;&nbsp;&nbsp;&nbsp;2、该新单词赋给$header。<BR>&nbsp;&nbsp;&nbsp;&nbsp;如果该新单词不该为第一个元素,则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>&nbsp;&nbsp;&nbsp;&nbsp;最后当处理完毕后,子程序print_list()依次输出链表,局域变量$pointer含有正在输出的值,$wordlist{$pointer}为下一个要输出的值。<BR>&nbsp;&nbsp;&nbsp;&nbsp;注:一般不需要用链表来做这些工作,用sort()和keys()在关联数组中循环就足够了,如:<BR></P>
      <BLOCKQUOTE>
        <P>foreach $word (sort keys(%wordlist)) {<BR>&nbsp; # print the sorted 
        list, or whatever } </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;但是,这里涉及的指针的概念在其它数据结构中很有意义。<BR><A 
      name=10.2>2、结构</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;许多编程语言可以定义结构(structure),即一组数据的集合。结构中的每个元素有其自己的名字,并通过该名字来访问。<BR>&nbsp;&nbsp;&nbsp;&nbsp;Perl不直接提供结构这种数据结构,但可以用关联数组来模拟。例如模拟C语言中如下的结构:<BR></P>
      <BLOCKQUOTE>
        <P>struce{<BR>&nbsp; int field1;<BR>&nbsp; int field2;<BR>&nbsp; int 
        field3; }mystructvar; </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;我们要做的是定义一个含有三个元素的关联数组,下标分别为field1、field2、field3,如:<BR></P>
      <BLOCKQUOTE>
        <P>%mystructvar = ("field1" , "" ,<BR>&nbsp; &nbsp; &nbsp; "field2" , "" 
        ,<BR>&nbsp; &nbsp; &nbsp; "field3" , "" ,); </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;像上面C语言的定义一样,这个关联数组%mystrctvar有三个元素,下标分别为field1、field2、field3,各元素初始值均为空串。对各元素的访问和赋值通过指定下标来进行,如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;$mystructvar{"field1"} 
      = 17;<BR><A 
      name=10.3>3、树</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;另一个经常使用的数据结构是树。树与链表类似,但每个节点指向的元素多于一个。最简单的树是二叉树,每个节点指向另外两个元素,称为左子节点和右子节点(或称孩子),每个子节点又指向两个孙子节点,依此类推。<BR>&nbsp;&nbsp;&nbsp;&nbsp;注:此处所说的树像上述链表一样是单向的,每个节点指向其子节点,但子节点并不指向父节点。<BR>&nbsp;&nbsp;&nbsp;&nbsp;树的概念可以如下描述:<BR></P>
      <BLOCKQUOTE>
        <UL>
          <LI>因为每个子节点均为一个树,所以左/右子节点也称为左/右子树。(有时称左/右分支) 
          <LI>第一个节点(不是任何节点的子节点的节点)称为树的根。 
          <LI>没有孩子(子节点)的节点称为叶节点。 </LI></UL></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;有多种使用关联数组实现树结构的方法,最好的一种应该是:给子节点分别加上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 :&nbsp; &nbsp; &nbsp; &nbsp; 
        &nbsp; "parentright", "child2",<BR>6 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
        "child1left", "grandchild1",<BR>7 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
        "child1right", "grandchild2",<BR>8 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
        "child2left", "grandchild3",<BR>9 :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
        "child2right", "grandchild4");<BR>10: # traverse tree, printing its 
        elements<BR>11: &amp;print_tree($rootname);<BR>12:<BR>13: sub print_tree 
        {<BR>14: &nbsp; local ($nodename) = @_;<BR>15: &nbsp; local 
        ($leftchildname, $rightchildname);<BR>16:<BR>17: &nbsp; $leftchildname = 
        $nodename . "left";<BR>18: &nbsp; $rightchildname = $nodename . 
        "right";<BR>19: &nbsp; if ($tree{$leftchildname} ne "") {<BR>20: &nbsp; 
        &nbsp; &amp;print_tree($tree{$leftchildname});<BR>21: &nbsp; }<BR>22: 
        &nbsp; print ("$nodename\n");<BR>23: &nbsp; if ($tree{$rightchildname} 
        ne "") {<BR>24: &nbsp; &nbsp; 
        &amp;print_tree($tree{$rightchildname});<BR>25: &nbsp; }<BR>26: } 
      </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;结果输出如下:<BR></P>
      <BLOCKQUOTE>
        <P>grandchild1<BR>child1<BR>grandchild2<BR>parent<BR>grandchild3<BR>child2<BR>grandchild4 
        </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;该程序创建的二叉树如下图:<BR></P>
      <P align=center><IMG height=126 
      src="Perl 语言-Perl 中文教程(第九章).files/tree.gif" width=253></P>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;注意函数print_tree()以次序“左子树、节点、右子树”来输出各节点的名字,这种遍历次序称为“左序遍历”。如果把第22行移到19行前,先输出节点明,再输出左子树、右子树,则为“中序遍历”,如果把第22行移到25行后,输出次序为左子树、右子树、节点,则为“右序遍历”。<BR>&nbsp;&nbsp;&nbsp;&nbsp;可以用同样的方法,即连接字符串构成下标,来创建其它的数据结构,如数据库等。</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>&copy; 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 + -