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

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

📁 perl的中文教程
💻 HTM
📖 第 1 页 / 共 3 页
字号:
            onmouseout=MM_swapImgRestore() 
            href="http://www.sun126.com/perl5/perl5-13.htm"><IMG 
            src="Perl 语言-Perl 中文教程(第九章).files/13.gif" border=0 
          name=Image16></A></TD></TR>
        <TR>
          <TD><A onmouseover="MM_swapImage('Image18','','14a.gif',1)" 
            onmouseout=MM_swapImgRestore() 
            href="http://www.sun126.com/perl5/perl5-14.htm"><IMG 
            src="Perl 语言-Perl 中文教程(第九章).files/14.gif" border=0 
          name=Image18></A></TD></TR>
        <TR>
          <TD><A onmouseover="MM_swapImage('Image19','','y1a.gif',1)" 
            onmouseout=MM_swapImgRestore() 
            href="http://www.sun126.com/perl5/perl5-15.htm"><IMG 
            src="Perl 语言-Perl 中文教程(第九章).files/y1.gif" border=0 
          name=Image19></A></TD></TR>
        <TR>
          <TD><A onmouseover="MM_swapImage('Image20','','y2a.gif',1)" 
            onmouseout=MM_swapImgRestore() 
            href="http://www.sun126.com/perl5/perl5-16.htm"><IMG 
            src="Perl 语言-Perl 中文教程(第九章).files/y2.gif" border=0 
          name=Image20></A></TD></TR>
        <TR>
          <TD><A onmouseover="MM_swapImage('Image21','','y3a.gif',1)" 
            onmouseout=MM_swapImgRestore() 
            href="http://www.sun126.com/perl5/perl5-17.htm"><IMG 
            src="Perl 语言-Perl 中文教程(第九章).files/y3.gif" border=0 
          name=Image21></A></TD></TR></TBODY></TABLE></TD>
    <TD class=myFont vAlign=top width=30><IMG height=1 
      src="Perl 语言-Perl 中文教程(第九章).files/x.gif" width=10> </TD>
    <TD class=myFont vAlign=top>
      <DIV align=center><IMG height=30 
      src="Perl 语言-Perl 中文教程(第九章).files/top.gif" width=180><BR><SPAN 
      class=myFont><SPAN class=myFont><FONT face=宋体>翻译:</FONT></SPAN></SPAN> 
      <SPAN class=myFont><SPAN class=myFont>flamephoenix</SPAN></SPAN> <BR>
      <HR width="100%" SIZE=1>

      <DIV align=left>
      <P align=center><B>第九章 关联数组/哈希表</B><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#1"> </A></P>
      <P><A href="http://www.sun126.com/perl5/perl5-9.htm#1">一、数组变量的限制</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#2">二、定义</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#3">三、访问关联数组的元素</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#4">四、增加元素</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#5">五、创建关联数组</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#6">六、从数组变量复制到关联数组</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#7">七、元素的增删</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#8">八、列出数组的索引和值</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#9">九、用关联数组循环</A><BR><A 
      href="http://www.sun126.com/perl5/perl5-9.htm#10">十、用关联数组创建数据结构</A><BR>&nbsp; 
      <A 
      href="http://www.sun126.com/perl5/perl5-9.htm#10.1">1、(单)链表</A><BR>&nbsp; 
      <A href="http://www.sun126.com/perl5/perl5-9.htm#10.2">2、结构</A><BR>&nbsp; 
      <A href="http://www.sun126.com/perl5/perl5-9.htm#10.3">3、树</A><BR><BR><A 
      name=1>一、数组变量的限制</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;在前面讲的数组变量中,可以通过下标访问其中的元素。例如,下列语句访问数组@array的第三个元素:<BR>&nbsp;&nbsp;&nbsp;&nbsp;$scalar 
      = 
      $array[2];<BR>&nbsp;&nbsp;&nbsp;&nbsp;虽然数组很有用,但它们有一个显著缺陷,即很难记住哪个元素存贮的什么内容。假如我们来写一个程序计算某文件中首字母大写的单词出现的次数,用数组来实现就比较困难,程序代码如下: 
      </P>
      <BLOCKQUOTE>
        <P>1 : #!/usr/local/bin/perl<BR>2 :<BR>3 : while ($inputline = 
        &lt;STDIN&gt;) {<BR>4 : &nbsp; while ($inputline =~ /\b[A-Z]\S+/g) 
        {<BR>5 : &nbsp; &nbsp; $word = $&amp;;<BR>6 : &nbsp; &nbsp; $word =~ 
        s/[;.,:-]$//; # remove punctuation<BR>7 : &nbsp; &nbsp; for ($count = 1; 
        $count &lt;= @wordlist;<BR>8 : &nbsp; &nbsp; &nbsp; &nbsp; $count++) 
        {<BR>9 : &nbsp; &nbsp; &nbsp; $found = 0;<BR>10: &nbsp; &nbsp; &nbsp; if 
        ($wordlist[$count-1] eq $word) {<BR>11: &nbsp; &nbsp; &nbsp; &nbsp; 
        $found = 1;<BR>12: &nbsp; &nbsp; &nbsp; &nbsp; $wordcount[$count-1] += 
        1;<BR>13: &nbsp; &nbsp; &nbsp; &nbsp; last;<BR>14: &nbsp; &nbsp; &nbsp; 
        }<BR>15: &nbsp; &nbsp; }<BR>16: &nbsp; &nbsp; if ($found == 0) {<BR>17: 
        &nbsp; &nbsp; &nbsp; $oldlength = @wordlist;<BR>18: &nbsp; &nbsp; &nbsp; 
        $wordlist[$oldlength] = $word;<BR>19: &nbsp; &nbsp; &nbsp; 
        $wordcount[$oldlength] = 1;<BR>20: &nbsp; &nbsp; }<BR>21: &nbsp; 
        }<BR>22: }<BR>23: print ("Capitalized words and number of 
        occurrences:\n");<BR>24: for ($count = 1; $count &lt;= @wordlist; 
        $count++) {<BR>25: &nbsp; print ("$wordlist[$count-1]: 
        $wordcount[$count-1]\n");<BR>26: }</P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;运行结果如下:<BR></P>
      <BLOCKQUOTE>
        <P>Here is a line of Input.<BR>This Input contains some Capitalized 
        words.<BR>^D<BR>Capitalized words and number of occurrences:<BR>Here: 
        1<BR>Input: 2<BR>This: 1<BR>Capitalized: 1 </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;这个程序每次从标准输入文件读一行文字,第四行起的循环匹配每行中首字母大写的单词,每找到一个循环一次,赋给简单变量$word。在第六行中去掉标点后,查看该单词是否曾出现过,7~15行中在@wordlist中挨个元素做此检查,如果某个元素与$word相等,@wordcount中相应的元素就增加一个数。如果没有出现过,即@wordlist中没有元素与$word相等,16~20行给@wordlist和@wordcount增加一个新元素。<BR><BR><A 
      name=2>二、定义</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;正如你所看到的,使用数组元素产生了一些问题。首先,@wordlist中哪个元素对应着哪个单词并不明显;更糟的是,每读进一个新单词,程序必须检查整个列表才能知道该单词是否曾经出现过,当列表变得较大时,这是很耗费时间的。<BR>&nbsp;&nbsp;&nbsp;&nbsp;这些问题产生的原因是数组元素通过数字下标访问,为了解决这类问题,Perl定义了另一种数组,可以用任意简单变量值来访问其元素,这种数组叫做关联数组,也叫哈希表。<BR>&nbsp;&nbsp;&nbsp;&nbsp;为了区分关联数组变量与普通的数组变量,Perl使用%作为其首字符,而数组变量以@打头。与其它变量名一样,%后的第一个字符必须为字母,后续字符可以为字母、数字或下划线。<BR><BR><A 
      name=3>三、访问关联数组的元素</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;关联数组的下标可以为任何简单/标量值,访问单个元素时以$符号打头,下标用大括号围起来。例如: 
      </P>
      <BLOCKQUOTE>
        <P>$fruit{"bananas"}<BR>$number{3.14159}<BR>$integer{-7} </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;简单变量也可作为下标,如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;$fruit{$my_fruit}<BR><BR><A 
      name=4>四、增加元素</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;创建一个关联数组元素最简单的方法是赋值,如语句$fruit{"bananas"} 
      = 1; 
      把1赋给关联数组%fruit下标为bananas的元素,如果该元素不存在,则被创建,如果数组%fruit从未使用过,也被创建。<BR>&nbsp;&nbsp;&nbsp;&nbsp;这一特性使得关联数组很容易用于计数。下面我们用关联数组改写上面的程序,注意实现同样的功能此程序简化了许多。<BR></P>
      <BLOCKQUOTE>
        <P>1 : #!/usr/local/bin/perl<BR>2 :<BR>3 : while ($inputline = <STDIN>) 
        {<BR>4 : &nbsp; while ($inputline =~ /\b[A-Z]\S+/g) {<BR>5 : &nbsp; 
        &nbsp; $word = $&amp;;<BR>6 : &nbsp; &nbsp; $word =~ s/[;.,:-]$//; # 
        remove punctuation<BR>7 : &nbsp; &nbsp; $wordlist{$word} += 1;<BR>8 : 
        &nbsp; }<BR>9 : }<BR>10: print ("Capitalized words and number of 
        occurrences:\n");<BR>11: foreach $capword (keys(%wordlist)) {<BR>12: 
        &nbsp; print ("$capword: $wordlist{$capword}\n");<BR>13: } 
      </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;运行结果如下:<BR></P>
      <BLOCKQUOTE>
        <P>Here is a line of Input.<BR>This Input contains some Capitalized 
        words.<BR>^D<BR>Capitalized words and number of occurrences:<BR>This: 
        1<BR>Input: 2<BR>Here: 1<BR>Capitalized: 1 </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;你可以看到,这次程序简单多了,读取输入并存贮各单词数目从20行减少到了7行。<BR>&nbsp;&nbsp;&nbsp;&nbsp;本程序用关联数组%wordlist跟踪首字母大写的单词,下标就用单词本身,元素值为该单词出现的次数。第11行使用了内嵌函数keys()。这个函数返回关联数组的下标列表,foreach语句就用此列表循环。<BR>&nbsp;&nbsp;&nbsp;&nbsp;注:关联数组总是随机存贮的,因此当你用keys()访问其所有元素时,不保证元素以任何顺序出现,特别值得一提的是,它们不会以被创建的顺序出现。<BR>&nbsp;&nbsp;&nbsp;&nbsp;要想控制关联数组元素出现的次序,可以用sort()函数对keys()返回值进行排列,如: 
      </P>
      <BLOCKQUOTE>
        <P>foreach $capword (sort keys(%wordlist)) {<BR>&nbsp; print ("$capword: 
        $wordlist{$capword}\n");<BR>} </P></BLOCKQUOTE>
      <P><A name=5></A><FONT 
      color=#003300>五、创建关联数组</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;可以用单个赋值语句创建关联数组,如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;%fruit 
      = 
      ("apples",17,"bananas",9,"oranges","none");<BR>&nbsp;&nbsp;&nbsp;&nbsp;此语句创建的关联数组含有下面三个元素: 
      </P>
      <BLOCKQUOTE>
        <UL>
          <LI>下标为apples的元素,值为17 
          <LI>下标为bananas的元素,值为9 
          <LI>下标为oranges的元素,值为none </LI></UL></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;注:用列表给关联数组赋值时,Perl5允许使用"=&gt;"或","来分隔下标与值,用"=&gt;"可读性更好些,上面语句等效于:<BR>&nbsp;&nbsp;&nbsp;&nbsp;%fruit 
      = ("apples"=&gt;17,"bananas"=&gt;9,"oranges"=&gt;"none");<BR><BR><A 
      name=6>六、从数组变量复制到关联数组</A><BR>&nbsp;&nbsp;&nbsp;&nbsp;与列表一样,也可以通过数组变量创建关联数组,当然,其元素数目应该为偶数,如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;@fruit 
      = 
      ("apples",17,"bananas",9,"oranges","none");<BR>&nbsp;&nbsp;&nbsp;&nbsp;%fruit 
      = 
      @fruit;<BR>&nbsp;&nbsp;&nbsp;&nbsp;反之,可以把关联数组赋给数组变量,如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;%fruit 
      = ("grapes",11,"lemons",27);<BR>&nbsp;&nbsp;&nbsp;&nbsp;@fruit = 
      %fruit;<BR>&nbsp;&nbsp;&nbsp;&nbsp;注意,此语句中元素次序未定义,那么数组变量@fruit可能为("grapes",11,"lemons",27)或("lemons",27,"grapes",11)。<BR>&nbsp;&nbsp;&nbsp;&nbsp;关联数组变量之间可以直接赋值,如:%fruit2 
      = %fruit1; 
      还可以把数组变量同时赋给一些简单变量和一个关联数组变量,如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;($var1, $var2, 
      %myarray) = 
      @list;<BR>&nbsp;&nbsp;&nbsp;&nbsp;此语句把@list的第一个元素赋给$var1,第二个赋给$var2,其余的赋给%myarray。<BR>&nbsp;&nbsp;&nbsp;&nbsp;最后,关联数组可以通过返回值为列表的内嵌函数或用户定义的子程序来创建,下例中把split()函数的返回值--一个列表--赋给一个关联数组变量。 
      </P>
      <BLOCKQUOTE>
        <P>1: #!/usr/local/bin/perl<BR>2:<BR>3: $inputline = 
        &lt;STDIN&gt;;<BR>4: $inputline =~ s/^\s+|\s+\n$//g;<BR>5: %fruit = 
        split(/\s+/, $inputline);<BR>6: print ("Number of bananas: 
        $fruit{\"bananas\"}\n"); </P></BLOCKQUOTE>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;运行结果如下: </P>
      <BLOCKQUOTE>
        <P>oranges 5 apples 7 bananas 11 cherries 6<BR>Number of bananas: 11 
      </P></BLOCKQUOTE>
      <P><A name=7></A><FONT 
      color=#003300>七、元素的增删</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;增加元素已经讲过,可以通过给一个未出现过的元素赋值来向关联数组中增加新元素,如$fruit{"lime"} 
      = 
      1;创建下标为lime、值为1的新元素。<BR>&nbsp;&nbsp;&nbsp;&nbsp;删除元素的方法是用内嵌函数delete,如欲删除上述元素,则:<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete 
      ($fruit{"lime"});<BR>注意: </P>
      <BLOCKQUOTE>
        <P>1、一定要使用delete函数来删除关联数组的元素,这是唯一的方法。<BR>2、一定不要对关联数组使用内嵌函数push、pop、shift及splice,因为其元素位置是随机的。 </P></BLOCKQUOTE>
      <P><A name=8></A><FONT 
      color=#003333>八、列出数组的索引和值</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;上面已经提到,keys()函数返回关联数组下标的列表,如:<BR></P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -