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

📄 ch11_07.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 11.6. Creating Arrays of Scalar References (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:40: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="ch11_01.htm"TITLE="11. References and Records"><LINKREL="prev"HREF="ch11_06.htm"TITLE="11.5. Taking References to Scalars"><LINKREL="next"HREF="ch11_08.htm"TITLE="11.7. Using Closures Instead of Objects"></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="ch11_06.htm"TITLE="11.5. Taking References to Scalars"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 11.5. Taking References to Scalars"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch11_01.htm"TITLE="11. References and Records"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_08.htm"TITLE="11.7. Using Closures Instead of Objects"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.7. Using Closures Instead of Objects"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch11-chap11_creating_0">11.6. Creating Arrays of Scalar References</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-716">Problem<ACLASS="indexterm"NAME="ch11-idx-1000004825-0"></A><ACLASS="indexterm"NAME="ch11-idx-1000004825-1"></A><ACLASS="indexterm"NAME="ch11-idx-1000004825-2"></A></A></H3><PCLASS="para">You want to create and manipulate an array of references to scalars. This arises when you pass variables by reference to a function to let the function change their values.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-722">Solution</A></H3><PCLASS="para">To create an array, either backslash each scalar in the list to store in the array:</P><PRECLASS="programlisting">@array_of_scalar_refs = ( \$a, \$b );</PRE><PCLASS="para">or simply backslash the entire list, taking advantage of the backslash operator's distributive property:</P><PRECLASS="programlisting">@array_of_scalar_refs = \( $a, $b );</PRE><PCLASS="para">To get or set the value of an element of the list, use <CODECLASS="literal">${</CODE> <CODECLASS="literal">...</CODE> <CODECLASS="literal">}</CODE>:</P><PRECLASS="programlisting">${ $array_of_scalar_refs[1] } = 12;         # $b = 12</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-738">Discussion</A></H3><PCLASS="para">In the following examples, <CODECLASS="literal">@array</CODE> is a simple array containing references to scalars (an array of references is not a reference to an array). To access the original data indirectly, braces are critical.</P><PRECLASS="programlisting">($a, $b, $c, $d) = (1 .. 4);        # initialize@array =  (\$a, \$b, \$c, \$d);     # refs to each scalar@array = \( $a,  $b,  $c,  $d);     # same thing!@array = map { \my $anon } 0 .. 3;  # allocate 4 anon scalarresf${ $array[2] } += 9;                # $c now 12${ $array[ $#array ] } *= 5;        # $d now 20${ $array[-1] }        *= 5;        # same; $d now 100$tmp   = $array[-1];                # using temporary$$tmp *= 5;                         # $d now 500</PRE><PCLASS="para">The two assignments to <CODECLASS="literal">@array</CODE> are equivalent &nbsp;-  the backslash operator is distributive across a list. So preceding a list (not an array) with a backslash is the same as applying a backslash to everything in that list. The ensuing code changes the values of the variables whose references were stored in the array.</P><PCLASS="para">Here's how to deal with such an array without explicit indexing.</P><PRECLASS="programlisting">use Math::Trig qw(pi);              # load the constant piforeach $sref (@array) {            # prepare to change $a,$b,$c,$d    ($$sref **= 3) *= (4/3 * pi);   # replace with spherical volumes}</PRE><PCLASS="para">This code uses the formula for deriving the volume of a sphere:</P><IMGCLASS="graphic"SRC="figs/eqn11_01.gif"ALT="Equation 11.1"><PCLASS="para">The <CODECLASS="literal">$sref</CODE> loop index variable is each reference in <CODECLASS="literal">@array</CODE>, and <CODECLASS="literal">$$sref</CODE> is the number itself, that is, the original variables <CODECLASS="literal">$a</CODE>, <CODECLASS="literal">$b</CODE>, <CODECLASS="literal">$c</CODE>, and <CODECLASS="literal">$d</CODE>. Changing <CODECLASS="literal">$$sref</CODE> in the loop changes those variables as well. First we replace <CODECLASS="literal">$$sref</CODE> with its cube, then multiply the resulting value by 4/3 <IMGSRC="../chars/pi.gif"ALT="[pi]">. This takes advantage of the fact that assignment in Perl returns an lvalue, letting you chain assignment operators together as we've done using the <CODECLASS="literal">**=</CODE> and <CODECLASS="literal">*=</CODE> assignment operators.</P><PCLASS="para">Actually, anonymous scalars are pretty useless, given that a scalar value fits in the same space as a scalar reference. That's why there's no explicit composer. Scalar references exist only to allow aliasing &nbsp;-  which can be done in other ways.<ACLASS="indexterm"NAME="ch11-idx-1000004827-0"></A><ACLASS="indexterm"NAME="ch11-idx-1000004827-1"></A><ACLASS="indexterm"NAME="ch11-idx-1000004827-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-788">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch02_05.htm#PERL2-CH-2-SECT-5.17">"Assignment Operators"</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 in <ICLASS="filename">perlop </I>(1)</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="ch11_06.htm"TITLE="11.5. Taking References to Scalars"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 11.5. Taking References to Scalars"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="ch11_08.htm"TITLE="11.7. Using Closures Instead of Objects"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.7. Using Closures Instead of Objects"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">11.5. Taking References to Scalars</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">11.7. Using Closures Instead of Objects</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 + -