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

📄 ch09_03.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Arrays of Hashes (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Arrays of Hashes"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch09_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch09_01.htm">Chapter 9: Data Structures</a></td><td align="right" valign="top" width="172"><a href="ch09_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">9.3. Arrays of Hashes</h2><p><a name="INDEX-2127"></a><a name="INDEX-2128"></a><a name="INDEX-2129"></a>An array of hashes is useful when you have a bunch of records thatyou'd like to access sequentially, and each record itself containskey/value pairs.  Arrays of hashes are used less frequently than theother structures in this chapter.</p><h3 class="sect2">9.3.1. Composition of an Array of Hashes</h3><p><a name="INDEX-2130"></a>You can create an array of anonymous hashes as follows:<blockquote><pre class="programlisting">@AoH = (    {       husband  =&gt; "barney",       wife     =&gt; "betty",       son      =&gt; "bamm bamm",    },    {       husband =&gt; "george",       wife    =&gt; "jane",       son     =&gt; "elroy",    },    {       husband =&gt; "homer",       wife    =&gt; "marge",       son     =&gt; "bart",    },  );</pre></blockquote>To add another hash to the array, you can simply say:<blockquote><pre class="programlisting">push @AoH, { husband =&gt; "fred", wife =&gt; "wilma", daughter =&gt; "pebbles" };</pre></blockquote></p><h3 class="sect2">9.3.2. Generation of an Array of Hashes</h3><p><a name="INDEX-2131"></a><a name="INDEX-2132"></a>Here are some techniques for populating an array of hashes.To read from a file with the following format:<blockquote><pre class="programlisting">husband=fred friend=barney</pre></blockquote>you could use either of the following two loops:<blockquote><pre class="programlisting">while ( &lt;&gt; ) {    $rec = {};    for $field ( split ) {        ($key, $value) = split /=/, $field;        $rec-&gt;{$key} = $value;    }    push @AoH, $rec;}while ( &lt;&gt; ) {    push @AoH, { split /[\s=]+/ };}</pre></blockquote>If you have a subroutine <tt class="literal">get_next_pair</tt> that returnskey/value pairs, you can use it to stuff <tt class="literal">@AoH</tt> with either ofthese two loops:<blockquote><pre class="programlisting">while ( @fields = get_next_pair() ) {    push @AoH, { @fields };}while (&lt;&gt;) {    push @AoH, { get_next_pair($_) };}</pre></blockquote><a name="INDEX-2133"></a>You can append new members to an existing hash like so:<blockquote><pre class="programlisting">$AoH[0]{pet} = "dino";$AoH[2]{pet} = "santa's little helper";</pre></blockquote></p><h3 class="sect2">9.3.3. Access and Printing of an Array of Hashes</h3><p><a name="INDEX-2134"></a><a name="INDEX-2135"></a><a name="INDEX-2136"></a>You can set a key/value pair of a particular hash as follows:<blockquote><pre class="programlisting">$AoH[0]{husband} = "fred";</pre></blockquote>To capitalize the husband of the second array, apply a substitution:<blockquote><pre class="programlisting">$AoH[1]{husband} =~ s/(\w)/\u$1/;</pre></blockquote>You can print all of the data as follows:<blockquote><pre class="programlisting">for $href ( @AoH ) {    print "{ ";    for $role ( keys %$href ) {         print "$role=$href-&gt;{$role} ";    }    print "}\n";}</pre></blockquote>and with indices:<blockquote><pre class="programlisting">for $i ( 0 .. $#AoH ) {    print "$i is { ";    for $role ( keys %{ $AoH[$i] } ) {         print "$role=$AoH[$i]{$role} ";    }    print "}\n";}</pre></blockquote></p><a name="INDEX-2137"></a><a name="INDEX-2138"></a><a name="INDEX-2139"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch09_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch09_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">9.2. Hashes of Arrays</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">9.4. Hashes of Hashes</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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