📄 ch04_12.htm
字号:
<HTML><HEAD><TITLE>Recipe 4.11. Processing Multiple Elements of an Array (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:31:57Z"><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="ch04_01.htm"TITLE="4. Arrays"><LINKREL="prev"HREF="ch04_11.htm"TITLE="4.10. Reversing an Array"><LINKREL="next"HREF="ch04_13.htm"TITLE="4.12. Finding the First List Element That Passes a Test"></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="ch04_11.htm"TITLE="4.10. Reversing an Array"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.10. Reversing an Array"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch04_01.htm"TITLE="4. Arrays"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_13.htm"TITLE="4.12. Finding the First List Element That Passes a Test"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.12. Finding the First List Element That Passes a Test"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch04-chap04_processing_0">4.11. Processing Multiple Elements of an Array</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-929">Problem <ACLASS="indexterm"NAME="ch04-idx-1000006694-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006694-1"></A></A></H3><PCLASS="para">You want to <CODECLASS="literal">pop</CODE><ACLASS="indexterm"NAME="ch04-idx-1000006708-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006708-1"></A> or <CODECLASS="literal">shift</CODE> multiple elements at a time.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-935">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">splice</CODE>:<ACLASS="indexterm"NAME="ch04-idx-1000006707-0"></A></P><PRECLASS="programlisting"># remove $N elements from front of @ARRAY (shift $N)@FRONT = splice(@ARRAY, 0, $N);# remove $N elements from the end of the array (pop $N)@END = splice(@ARRAY, -$N);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-951">Discussion</A></H3><PCLASS="para">It's often convenient to wrap these as functions:</P><PRECLASS="programlisting">sub shift2 (\@) { return splice(@{$_[0]}, 0, 2);}sub pop2 (\@) { return splice(@{$_[0]}, -2);}</PRE><PCLASS="para">This makes their behavior more apparent when you use them:</P><PRECLASS="programlisting">@friends = qw(Peter Paul Mary Jim Tim);($this, $that) = shift2(@friends);# $this contains Peter, $that has Paul, and# @friends has Mary, Jim, and Tim@beverages = qw(Dew Jolt Cola Sprite Fresca);@pair = pop2(@beverages);# $pair[0] contains Sprite, $pair[1] has Fresca,# and @beverages has (Dew, Jolt, Cola)</PRE><PCLASS="para"><CODECLASS="literal">splice</CODE> returns the elements removed from the array, so <CODECLASS="literal">shift2</CODE> replaces the first two elements in <CODECLASS="literal">@ARRAY</CODE> with nothing (i.e., deletes them) and returns the two elements it deleted. In <CODECLASS="literal">pop2</CODE>, the last two elements at end of the array are removed and returned.</P><PCLASS="para">These two functions are prototyped to take an array reference as their argument to better mimic the built-in <CODECLASS="literal">shift</CODE> and <CODECLASS="literal">pop</CODE> functions. The caller doesn't pass in an explicit reference using a backslash. Instead, the compiler, having seen the array reference prototype, arranges to pass the array by reference anyway. Advantages to this approach include efficiency, transparency, and compile-time parameter checking. One disadvantage is that the thing passed in must look like a real array with a leading <CODECLASS="literal">@</CODE> sign, not just a scalar containing an array reference. If it did, you'd have to prepend an <CODECLASS="literal">@</CODE>, making it less transparent:</P><PRECLASS="programlisting">$line[5] = \@list;@got = pop2( @{ $line[5] } );</PRE><PCLASS="para">This is another example of where a proper array and not a mere list is called for. The <CODECLASS="literal">\@</CODE> prototype requires that whatever goes in that argument slot be an array. <CODECLASS="literal">$line[5]</CODE> isn't an array, but an array reference. That's why we need the "extra" <CODECLASS="literal">@</CODE> sign.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-1001">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">splice</CODE> function in <EMCLASS="emphasis">perlfunc </EM>(1) and <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>; the "Prototypes" sections of <ICLASS="filename">perlsub </I>(1) and <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>; we use <CODECLASS="literal">splice</CODE> in <ACLASS="xref"HREF="ch04_10.htm"TITLE="Appending One Array to Another">Recipe 4.9</A> <CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch04-idx-1000007437-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000007437-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000007438-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000007438-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000007438-2"></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="ch04_11.htm"TITLE="4.10. Reversing an Array"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.10. Reversing an Array"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="ch04_13.htm"TITLE="4.12. Finding the First List Element That Passes a Test"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.12. Finding the First List Element That Passes a Test"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">4.10. Reversing an Array</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">4.12. Finding the First List Element That Passes a Test</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 + -