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

📄 ch03_15.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Range Operator (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Range Operator"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_14.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch03_01.htm">Chapter 3: Unary and Binary Operators</a></td><td align="right" valign="top" width="172"><a href="ch03_16.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">3.15. Range Operator</h2><p><a name="INDEX-974"></a><a name="INDEX-975"></a><a name="INDEX-976"></a>The <tt class="literal">..</tt> range operator is really two different operatorsdepending on the context.</p><p><a name="INDEX-977"></a></p><p>In scalar context, <tt class="literal">..</tt> returns a Boolean value.  Theoperator is bi-stable, like an electronic flip-flop, and emulates theline-range (comma) operator of <em class="emphasis">sed</em>,<em class="emphasis">awk</em>, and various editors.  Each scalar<tt class="literal">..</tt> operator maintains its own Boolean state.  It isfalse as long as its left operand is false.  Once the left operand istrue, the range operator stays true until the right operand is true,<em class="emphasis">after</em> which the range operator becomes falseagain.  The operator doesn't become false until the next time it isevaluated.  It can test the right operand and become false on the sameevaluation as the one where it became true (the way<em class="emphasis">awk</em>'s range operator behaves), but itstill returns true once.  If you don't want it to test the rightoperand until the next evaluation (which is how<em class="emphasis">sed</em>'s range operator works), just usethree dots (<tt class="literal">...</tt>) instead of two.  With both<tt class="literal">..</tt> and <tt class="literal">...</tt>, the right operand isnot evaluated while the operator is in the false state, and the leftoperand is not evaluated while the operator is in the true state.</p><p><a name="INDEX-978"></a><a name="INDEX-979"></a>The value returned is either the null string for false or a sequencenumber (beginning with <tt class="literal">1</tt>) for true.  The sequencenumber is reset for each range encountered. The final sequence numberin a range has the string "<tt class="literal">E0</tt>" appended to it,which doesn't affect its numeric value, but gives you something tosearch for if you want to exclude the endpoint.  You can exclude thebeginning point by waiting for the sequence number to be greater than1. If either operand of scalar <tt class="literal">..</tt> is a numericliteral, that operand implicitly compared to the <tt class="literal">$.</tt>variable, which contains the current line number for your input file.Examples:<blockquote><pre class="programlisting">if (101 .. 200) { print; }  # print 2nd hundred linesnext line if (1 .. /^$/);   # skip header liness/^/&gt; / if (/^$/ .. eof()); # quote body</pre></blockquote>In list context, <tt class="literal">..</tt> returns a list of valuescounting (by ones) from the left value to the right value.  This isuseful for writing <tt class="literal">for (1..10)</tt> loops and for doingslice operations on arrays:<blockquote><pre class="programlisting">for (101 .. 200) { print; }            # prints 101102...199200@foo = @foo[0 .. $#foo];               # an expensive no-op@foo = @foo[ -5 .. -1];                # slice last 5 items</pre></blockquote>If the left value is greater than the right value, a null list isreturned.  (To produce a list in reverse order, see the <tt class="literal">reverse</tt> operator.)</p><p><a name="INDEX-980"></a><a name="INDEX-981"></a>If its operands are strings, the range operator makes use of the magical autoincrement algorithm discussed earlier.<a href="#FOOTNOTE-4">[4]</a> So you can say:<blockquote><pre class="programlisting">@alphabet = ('A' .. 'Z');</pre></blockquote>to get all the letters of the (English) alphabet, or:<blockquote><pre class="programlisting">$hexdigit = (0 .. 9, 'a' .. 'f')[$num &amp; 15];</pre></blockquote>to get a hexadecimal digit, or:<blockquote><pre class="programlisting">@z2 = ('01' .. '31');  print $z2[$mday];</pre></blockquote>to get dates with leading zeros.  You can also say:<blockquote><pre class="programlisting">@combos = ('aa' .. 'zz');</pre></blockquote>to get all combinations of two lowercase letters. However, be careful ofsomething like:<blockquote><pre class="programlisting">@bigcombos = ('aaaaaa' .. 'zzzzzz');</pre></blockquote>since that will require lots of memory.  More precisely, it'll needspace to store 308,915,776 scalars.  Let's hope you allocated a very largeswap partition.  Perhaps you should consider an iterative approachinstead.</p><blockquote class="footnote"><a name="FOOTNOTE-4"></a><p>[4] If the final value specified is notin the sequence that the magical increment would produce, the sequencecontinues until the next value is longer than the final valuespecified.</p></blockquote><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_14.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch03_16.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.14. C-Style Logical (Short-Circuit) Operators</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">3.16. Conditional Operator</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -