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

📄 ch05_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 5.5. Printing a Hash (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:32:36Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch05_01.htm"TITLE="5. Hashes"><LINKREL="prev"HREF="ch05_05.htm"TITLE="5.4. Traversing a Hash"><LINKREL="next"HREF="ch05_07.htm"TITLE="5.6. Retrieving from a Hash in Insertion Order"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch05_05.htm"TITLE="5.4. Traversing a Hash"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 5.4. Traversing a Hash"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch05_01.htm"TITLE="5. Hashes"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch05_07.htm"TITLE="5.6. Retrieving from a Hash in Insertion Order"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 5.6. Retrieving from a Hash in Insertion Order"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch05-chap05_printing_0">5.5. Printing a Hash</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-528">Problem <ACLASS="indexterm"NAME="ch05-idx-1000006456-0"></A><ACLASS="indexterm"NAME="ch05-idx-1000006456-1"></A></A></H3><PCLASS="para">You want to print a hash, but neither <CODECLASS="literal">print</CODE> <CODECLASS="literal">&quot;%hash&quot;</CODE> nor <CODECLASS="literal">print</CODE> <CODECLASS="literal">%hash</CODE> works.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-534">Solution</A></H3><PCLASS="para">One of several approaches is to iterate over every key-value pair in the hash using <ACLASS="xref"HREF="ch05_05.htm"TITLE="Traversing a Hash">Recipe 5.4</A>, and print them:</P><PRECLASS="programlisting">while ( ($k,$v) = each %hash ) {    print &quot;$k =&gt; $v\n&quot;;}</PRE><PCLASS="para">Or use <CODECLASS="literal">map</CODE> to generate a list of strings:</P><PRECLASS="programlisting">print map { &quot;$_ =&gt; $hash{$_}\n&quot; } keys %hash;</PRE><PCLASS="para">Or use the interpolation trick from <ACLASS="xref"HREF="ch01_11.htm"TITLE="Interpolating Functions and Expressions Within Strings">Recipe 1.10</A> to interpolate the hash as a list:</P><PRECLASS="programlisting">print &quot;@{[ %hash ]}\n&quot;;</PRE><PCLASS="para">Or use a temporary array variable to hold the hash, and print that:</P><PRECLASS="programlisting">{    my @temp = %hash;    print &quot;@temp&quot;;}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-564">Discussion</A></H3><PCLASS="para">The methods differ in the degree that their output is customizable in order and formatting and in their efficiency.</P><PCLASS="para">The first method, iterating over the hash, is very flexible and space-efficient. You can format the output as you like it, and it only requires two scalar variables: the current key and value. You can print the hash in key order (at the cost of building a list of sorted keys) if you use a <CODECLASS="literal">foreach</CODE> loop.</P><PRECLASS="programlisting">foreach $k (sort keys %hash) {    print &quot;$k =&gt; $hash{$k}\n&quot;;}</PRE><PCLASS="para">The <CODECLASS="literal">map</CODE> function is just as flexible. You can still process the list in any order by sorting the keys. You can customize the output to your heart's content. But it builds up a list of strings like <CODECLASS="literal">&quot;KEY</CODE> <CODECLASS="literal">=&gt;VALUE\n&quot;</CODE> to pass to <CODECLASS="literal">print</CODE>.</P><PCLASS="para">The last two methods are interpolation tricks. By treating the hash as an list, you can't predict or control the output order of the key-value pairs. Furthermore, the output will consist of a list of keys and values, each separated by whatever <CODECLASS="literal">$&quot;</CODE> happens to hold. You can't put newlines between pairs or <CODECLASS="literal">&quot;=&gt;&quot;</CODE> within them, as we could with the other methods.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-582">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">$&quot;</CODE> variable in <EMCLASS="emphasis">perlvar  </EM>(1) and in the <ACLASS="olink"HREF="../prog/ch02_09.htm#PERL2-CH-2-SECT-9.3">"Global Special Variables"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the <CODECLASS="literal">foreach</CODE>, <CODECLASS="literal">map</CODE>, <CODECLASS="literal">keys</CODE>, <CODECLASS="literal">sort</CODE>, and <CODECLASS="literal">each</CODE> functions in <ICLASS="filename">perlfunc </I>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; we give a technique for interpolating into strings in <ACLASS="xref"HREF="ch01_11.htm"TITLE="Interpolating Functions and Expressions Within Strings">Recipe 1.10</A>; we discuss the techniques for hash traversal in <ACLASS="xref"HREF="ch05_05.htm"TITLE="Traversing a Hash">Recipe 5.4</A><ACLASS="indexterm"NAME="ch05-idx-1000006458-0"></A><ACLASS="indexterm"NAME="ch05-idx-1000006458-1"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch05_05.htm"TITLE="5.4. Traversing a Hash"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 5.4. Traversing a Hash"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch05_07.htm"TITLE="5.6. Retrieving from a Hash in Insertion Order"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 5.6. Retrieving from a Hash in Insertion Order"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">5.4. Traversing a Hash</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">5.6. Retrieving from a Hash in Insertion Order</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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