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

📄 ch14_05.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 14.4. Merging DBM Files (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:42:48Z"><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="ch14_01.htm"TITLE="14. Database Access"><LINKREL="prev"HREF="ch14_04.htm"TITLE="14.3. Converting Between DBM Files"><LINKREL="next"HREF="ch14_06.htm"TITLE="14.5. Locking DBM Files"></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="ch14_04.htm"TITLE="14.3. Converting Between DBM Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.3. Converting Between DBM Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch14_01.htm"TITLE="14. Database Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_06.htm"TITLE="14.5. Locking DBM Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.5. Locking DBM Files"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-16765">14.4. Merging DBM Files</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-402">Problem<ACLASS="indexterm"NAME="ch14-idx-1000004951-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004951-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004951-2"></A></A></H3><PCLASS="para">You want to combine two DBM files into a single DBM file with original key/value pairs.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-408">Solution</A></H3><PCLASS="para">Either merge the databases by treating their hashes as lists:</P><PRECLASS="programlisting">%OUTPUT = (%INPUT1, %INPUT2);</PRE><PCLASS="para">or, more wisely, by iterating over each key-value pair.</P><PRECLASS="programlisting">%OUTPUT = ();foreach $href ( \%INPUT1, \%INPUT2 ) {    while (my($key, $value) = each(%$href)) {        if (exists $OUTPUT{$key}) {            # decide which value to use and set $OUTPUT{$key} if necessary        } else {            $OUTPUT{$key} = $value;        }    }}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-442">Discussion</A></H3><PCLASS="para">This straightforward application of <ACLASS="xref"HREF="ch05_11.htm"TITLE="Merging Hashes">Recipe 5.10</A> comes with the same caveats. Merging hashes by treating them as lists requires that the hashes be preloaded into memory, creating a potentially humongous temporary list. If you're dealing with large hashes, have little virtual memory, or both, then you want to iterate over the keys with <CODECLASS="literal">each</CODE> to save memory.</P><PCLASS="para">Another difference between these merging techniques is what to do if the same key exists in both input databases. The blind assignment merely overwrites the first value with the second value. The iterative merging technique lets you decide what to do. Possibilities include issuing a warning or error, choosing the first over the second, choosing the second over the first, or concatenating the new value to the old one. If you're using the MLDBM module, you can even store them both, using an array reference to the two values.<ACLASS="indexterm"NAME="ch14-idx-1000004953-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004953-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004953-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-450">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch05_11.htm"TITLE="Merging Hashes">Recipe 5.10</A>; <ACLASS="xref"HREF="ch14_09.htm"TITLE="Storing Complex Data in a DBM File">Recipe 14.8</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="ch14_04.htm"TITLE="14.3. Converting Between DBM Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.3. Converting Between DBM Files"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="ch14_06.htm"TITLE="14.5. Locking DBM Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.5. Locking DBM Files"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.3. Converting Between DBM Files</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">14.5. Locking DBM Files</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 + -