📄 ch14_07.htm
字号:
<HTML><HEAD><TITLE>Recipe 14.6. Sorting Large DBM Files (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:42:51Z"><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_06.htm"TITLE="14.5. Locking DBM Files"><LINKREL="next"HREF="ch14_08.htm"TITLE="14.7. Treating a Text File as a Database Array"></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_06.htm"TITLE="14.5. Locking DBM Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.5. Locking 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_08.htm"TITLE="14.7. Treating a Text File as a Database Array"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.7. Treating a Text File as a Database Array"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-41357">14.6. Sorting Large DBM Files</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-604">Problem<ACLASS="indexterm"NAME="ch14-idx-1000004972-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004972-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004972-2"></A></A></H3><PCLASS="para">You want to process a large dataset you'd like to commit to a DBM file in a particular order.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-610">Solution</A></H3><PCLASS="para">Use the <ACLASS="indexterm"NAME="ch14-idx-1000004973-0"></A>DB_File's B-tree bindings and supply a comparison function of your own devising:</P><PRECLASS="programlisting">use DB_File;# specify the Perl sub to do key comparison using the# exported $DB_BTREE hash reference$DB_BTREE->{'compare'} = sub { my ($key1, $key2) = @_ ; "\L$key1" cmp "\L$key2" ;};tie(%hash, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE) or die "can't tie $filename: $!";</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-638">Description</A></H3><PCLASS="para">An annoyance of hashes, whether in memory or as DBM files, is that they do not maintain proper ordering. The CPAN module Tie::IxHash can make a regular hash in memory maintain its insertion order, but that doesn't help you for DBM databases or arbitrary sorting criteria.</P><PCLASS="para">The DB_File module supports a nice solution to this using a <ACLASS="indexterm"NAME="ch14-idx-1000004974-0"></A>B-tree implementation. One advantage of a B-tree over a regular DBM hash is its ordering. When the user defines a comparison function, all calls to <CODECLASS="literal">keys</CODE>, <CODECLASS="literal">values</CODE>, and <CODECLASS="literal">each</CODE> are automatically ordered. For example, <ACLASS="xref"HREF="ch14_07.htm#ch14-17113"TITLE="sortdemo">Example 14.4</A> is a program that maintains a hash whose keys will always be sorted case-insensitively.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch14-17113">Example 14.4: sortdemo</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# <ACLASS="indexterm"NAME="ch14-idx-1000005040-0"></A>sortdemo - show auto dbm sortinguse strict;use DB_File;$DB_BTREE->{'compare'} = sub { my ($key1, $key2) = @_ ; "\L$key1" cmp "\L$key2" ;};my %hash;my $filename = '/tmp/sorthash.db';tie(%hash, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE) or die "can't tie $filename: $!";my $i = 0;for my $word (qw(Can't you go camp down by Gibraltar)) { $hash{$word} = ++$i;}while (my($word, $number) = each %hash) { printf "%-12s %d\n", $word, $number;}</PRE></DIV><PCLASS="para">By default, the entries in a B-tree DB_File database are stored alphabetically. Here, though, we provide a case-insensitive comparison function, so using <CODECLASS="literal">each</CODE> to fetch all the keys would show:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>by 6</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>camp 4</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Can't 1</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>down 5</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Gibraltar 7</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>go 3</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>you 2</I></CODE></B></CODE></PRE><PCLASS="para">This sorting property on hashes is so convenient that it's worth using even without a permanent database. If you pass <CODECLASS="literal">undef</CODE> where the filename is expected on the <CODECLASS="literal">tie</CODE>, DB_File will create a file in <EMCLASS="emphasis">/tmp</EM> and then immediately unlink it, giving an anonymous database:</P><PRECLASS="programlisting">tie(%hash, "DB_File", undef, O_RDWR|O_CREAT, 0666, $DB_BTREE) or die "can't tie: $!";</PRE><PCLASS="para">Remember these two things if you supply a comparison for your BTREE database. One, the new compare function must be specified when you create the database. Two, you cannot change the ordering once the database has been created; you must use the same compare function every time you access the database.</P><PCLASS="para">Using BTREE databases under DB_File also permits duplicate or partial keys. See its documentation for examples.<ACLASS="indexterm"NAME="ch14-idx-1000004976-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004976-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004976-2"></A><ACLASS="indexterm"NAME="ch14-idx-1000004976-3"></A><ACLASS="indexterm"NAME="ch14-idx-1000004976-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-1000004725">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch05_07.htm"TITLE="Retrieving from a Hash in Insertion Order">Recipe 5.6</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_06.htm"TITLE="14.5. Locking DBM Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.5. Locking 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_08.htm"TITLE="14.7. Treating a Text File as a Database Array"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.7. Treating a Text File as a Database Array"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.5. Locking 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.7. Treating a Text File as a Database Array</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 © 2002</a> O'Reilly & 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 + -