ch10_06.htm

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

HTM
388
字号
<HTML><HEAD><TITLE>Recipe 10.5. Passing Arrays and Hashes by Reference (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:38Z"><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="ch10_01.htm"TITLE="10. Subroutines"><LINKREL="prev"HREF="ch10_05.htm"TITLE="10.4. Determining Current Function Name"><LINKREL="next"HREF="ch10_07.htm"TITLE="10.6. Detecting Return Context"></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="ch10_05.htm"TITLE="10.4. Determining Current Function Name"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.4. Determining Current Function Name"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch10_01.htm"TITLE="10. Subroutines"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_07.htm"TITLE="10.6. Detecting Return Context"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.6. Detecting Return Context"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch10-33402">10.5. Passing Arrays and Hashes by Reference</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-428">Problem<ACLASS="indexterm"NAME="ch10-idx-1000004676-0"></A><ACLASS="indexterm"NAME="ch10-idx-1000004676-1"></A><ACLASS="indexterm"NAME="ch10-idx-1000004676-2"></A><ACLASS="indexterm"NAME="ch10-idx-1000004676-3"></A><ACLASS="indexterm"NAME="ch10-idx-1000004676-4"></A><ACLASS="indexterm"NAME="ch10-idx-1000004676-5"></A></A></H3><PCLASS="para">You want to pass a function more than one array or hash and have each remain distinct. For example, you want to put the "Find elements in one array but not in another" algorithm from <ACLASS="xref"HREF="ch04_08.htm"TITLE="Finding Elements in One Array but Not Another">Recipe 4.7</A> in a subroutine. This subroutine must then be called with two arrays that remain distinct.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-434">Solution</A></H3><PCLASS="para">Pass arrays and hashes by reference, using the backslash operator:</P><PRECLASS="programlisting">array_diff( \@array1, \@array2 );</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-442">Discussion</A></H3><PCLASS="para">See <ACLASS="xref"HREF="ch11_01.htm"TITLE="References and Records">Chapter 11, <CITECLASS="chapter">References and Records</CITE></A>, for more about manipulation of references. Here's a subroutine that takes array references and a subroutine call that generates them:</P><PRECLASS="programlisting">@a = (1, 2);@b = (5, 8);@c = add_vecpair( \@a, \@b );print &quot;@c\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>6 10</I></CODE></B></CODE> sub add_vecpair {       # assumes both vectors the same length    my ($x, $y) = @_;   # copy in the array references    my @result;    for (my $i=0; $i &lt; @$x; $i++) {      $result[$i] = $x-&gt;[$i] + $y-&gt;[$i];    }    return @result;}</PRE><PCLASS="para">A potential difficulty with this function is that it doesn't check to make sure it got exactly two arguments that were both array references. You could check explicitly this way:</P><PRECLASS="programlisting">unless (@_ == 2 &amp;&amp; ref($x) eq 'ARRAY' &amp;&amp; ref($y) eq 'ARRAY') {    die &quot;usage: add_vecpair ARRAYREF1 ARRAYREF2&quot;;}</PRE><PCLASS="para">If all you plan to do is <CODECLASS="literal">die</CODE> on error (see <ACLASS="xref"HREF="ch10_13.htm"TITLE="Handling Exceptions">Recipe 10.12</A>), you can usually omit this check, since dereferencing the wrong kind of reference triggers an exception anyway.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch10-pgfId-484">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch02_07.htm#PERL2-CH-2-SECT-7.2">"Passing References"</A> in <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> and on "Pass by Reference" in <ICLASS="filename">perlsub </I>(1); the section on <ACLASS="olink"HREF="../prog/ch02_07.htm#PERL2-CH-2-SECT-7.3">"Prototypes"</A> in <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> or in <ICLASS="filename">perlsub </I>(1); <ACLASS="xref"HREF="ch10_12.htm"TITLE="Prototyping Functions">Recipe 10.11</A>; <ACLASS="xref"HREF="ch11_01.htm"TITLE="References and Records">Chapter 11</A>; <ACLASS="olink"HREF="../prog/ch04_01.htm">Chapter 4</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</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="ch10_05.htm"TITLE="10.4. Determining Current Function Name"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.4. Determining Current Function Name"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="ch10_07.htm"TITLE="10.6. Detecting Return Context"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 10.6. Detecting Return Context"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">10.4. Determining Current Function Name</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">10.6. Detecting Return Context</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 + -
显示快捷键?