📄 ch03_03.htm
字号:
<html><head><title>Autoincrement and Autodecrement (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Autoincrement and Autodecrement"><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_02.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_04.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.3. Autoincrement and Autodecrement</h2><p><a name="INDEX-817"></a><a name="INDEX-818"></a><a name="INDEX-819"></a><a name="INDEX-820"></a> The<tt class="literal">++</tt> and <tt class="literal">--</tt> operators workas in C. That is, when placed before a variable, they increment ordecrement the variable before returning the value, and when placedafter, they increment or decrement the variable after returning thevalue. For example, <tt class="literal">$a++</tt> increments the value ofscalar variable <tt class="literal">$a</tt>, returning the value<em class="emphasis">before</em> it performs the increment. Similarly,<tt class="literal">--$b{(/(\w+)/)[0]}</tt> decrements the elementof the hash <tt class="literal">%b</tt> indexed by the first "word" in thedefault search variable (<tt class="literal">$_</tt>) and returnsthe value <em class="emphasis">after</em> the decrement.<a href="#FOOTNOTE-2">[2]</a></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2]Okay, so that wasn't exactly fair. We just wanted to make sure youwere paying attention. Here's how that expression works. First thepattern match finds the first word in <tt class="literal">$_</tt> using theregular expression <tt class="literal">\w+</tt>. The parentheses aroundthat cause the word to be returned as a single-element list valuebecause the pattern match is in a list context. The list context issupplied by the list slice operator, <tt class="literal">(...)[0]</tt>,which returns the first (and only) element of the list. That value isused as the key for the hash, and the hash entry (value) isdecremented and returned. In general, when confronted with a complexexpression, analyze it from the inside out to see what order thingshappen in.</p></blockquote><p><a name="INDEX-821"></a><a name="INDEX-822"></a>The autoincrement operator has a little extra built-in magic. Ifyou increment a variable that is numeric, or that has ever been used ina numeric context, you get a normal increment. If, however, thevariable has only been used in string contexts since it was set, hasa value that is not the null string, and matches the pattern <tt class="literal">/^[a-zA-Z]*[0-9]*$/</tt>,the increment is done as a string, preserving each character within itsrange, with carry:<blockquote><pre class="programlisting">print ++($foo = '99'); # prints '100'print ++($foo = 'a0'); # prints 'b1'print ++($foo = 'Az'); # prints 'Ba'print ++($foo = 'zz'); # prints 'aaa'</pre></blockquote>As of this writing, magical autoincrement has not been extended to Unicodeletters and digits, but it might be in the future.</p><p>The autodecrement operator, however, is not magical, and we have no plansto make it so.</p><!-- 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_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.2. The Arrow Operator</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.4. Exponentiation</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 © 2001</a> O'Reilly & 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 + -