ch09_05.htm

来自「By Tom Christiansen and Nathan Torkingto」· HTM 代码 · 共 381 行

HTM
381
字号
<HTML><HEAD><TITLE>Recipe 9.4. Recognizing Two Names for the Same File (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:39:06Z"><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="ch09_01.htm"TITLE="9. Directories"><LINKREL="prev"HREF="ch09_04.htm"TITLE="9.3. Copying or Moving a File"><LINKREL="next"HREF="ch09_06.htm"TITLE="9.5. Processing All Files in a Directory"></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="ch09_04.htm"TITLE="9.3. Copying or Moving a File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.3. Copying or Moving a File"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch09_01.htm"TITLE="9. Directories"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_06.htm"TITLE="9.5. Processing All Files in a Directory"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.5. Processing All Files in a Directory"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch09-chap09_recognizing_0">9.4. Recognizing Two Names for the Same File</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-462">Problem<ACLASS="indexterm"NAME="ch09-idx-1000004376-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004376-1"></A></A></H3><PCLASS="para">You want to identify if two filenames in a list correspond to the same file on disk (because of hard and soft links, two filenames can refer to a single file). You might do this to make sure that you don't change a file you've already worked with.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-468">Solution</A></H3><PCLASS="para">Maintain a hash, keyed by the device and inode number of the files you've seen. The values are the names of the files:</P><PRECLASS="programlisting">%seen = ();sub do_my_thing {    my $filename = shift;    my ($dev, $ino) = stat $filename;    unless ($seen{$dev, $ino}++) {        # do something with $filename because we haven't        # seen it before    }}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-496">Discussion</A></H3><PCLASS="para">A key in <CODECLASS="literal">%seen</CODE> is made by combining the device number (<CODECLASS="literal">$dev</CODE>) and inode number (<CODECLASS="literal">$ino</CODE>) of each file. Files that are the same will have the same device and inode numbers, so they will have the same key.</P><PCLASS="para">If you want to maintain a list of all files of the same name, instead of counting the number of times seen, save the name of the file in an anonymous array.</P><PRECLASS="programlisting">foreach $filename (@files) {    ($dev, $ino) = stat $filename;    push( @{ $seen{$dev,$ino} }, $filename);}foreach $devino (sort keys %seen) {    ($dev, $ino) = split(/$;/o, $devino);    if (@{$seen{$devino}} &gt; 1) {        # @{$seen{$devino}} is a list of filenames for the same file    }}</PRE><PCLASS="para">The <CODECLASS="literal">$;</CODE><ACLASS="indexterm"NAME="ch09-idx-1000005018-0"></A> variable contains the separator string using the old <ACLASS="indexterm"NAME="ch09-idx-1000005017-0"></A>multidimensional associative array emulation syntax,<SPANCLASS="acronym"> </SPAN><CODECLASS="literal">$hash{$x,$y,$z}</CODE>. It's still a one-dimensional hash, but it has composite keys. The key is really <CODECLASS="literal">join($;</CODE> <CODECLASS="literal">=&gt;</CODE> <CODECLASS="literal">$x,</CODE> <CODECLASS="literal">$y,</CODE> <CODECLASS="literal">$z)</CODE>. The <CODECLASS="literal">split</CODE> separates them again. Although you'd normally just use a real multilevel hash directly, here there's no need, and it's cheaper not to.<ACLASS="indexterm"NAME="ch09-idx-1000004378-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004378-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-528">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">$;</CODE> variable in <ICLASS="filename">perlvar </I>(1), and in the <ACLASS="olink"HREF="../prog/ch02_09.htm">"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">stat</CODE> function in <ICLASS="filename">perlfunc </I>(1) and in <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>; <ACLASS="xref"HREF="ch05_01.htm"TITLE="Hashes">Chapter 5, <CITECLASS="chapter">Hashes</CITE></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="ch09_04.htm"TITLE="9.3. Copying or Moving a File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.3. Copying or Moving a File"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="ch09_06.htm"TITLE="9.5. Processing All Files in a Directory"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.5. Processing All Files in a Directory"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">9.3. Copying or Moving a File</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">9.5. Processing All Files in a Directory</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 + =
减小字号Ctrl + -
显示快捷键?