📄 ch02_06.htm
字号:
<td> </td><td><p><tt class="literal">==</tt> <tt class="literal">!=</tt><tt class="literal"><=></tt> <tt class="literal">eq</tt><tt class="literal">ne</tt> <tt class="literal">cmp</tt> (the "equal"ones)</p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">&</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">|</tt> <tt class="literal">^</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">&&</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">||</tt></p></td></tr><tr><td> </td><td><p><tt class="literal">..</tt> <tt class="literal">...</tt></p></td></tr><tr><td><p>right</p></td><td><p><tt class="literal">?:</tt> (ternary)</p></td></tr><tr><td><p>right</p></td><td><p><tt class="literal">=</tt> <tt class="literal">+=</tt> <tt class="literal">-=</tt><tt class="literal">.=</tt> (and similar assignment operators)</p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">,</tt> <tt class="literal">=></tt></p></td></tr><tr><td> </td><td><p>list operators (rightward)</p></td></tr><tr><td><p>right</p></td><td><p><tt class="literal">not</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">and</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">or</tt> <tt class="literal">xor</tt></p></td></tr></table><p><p>In the chart, any given operator has higher precedence than all ofthe operators listed below it, and lower precedence than all of theoperators listed above it. Operators at the same precedence levelresolve according to rules of <em class="emphasis">associativity</em>instead.</p><p>Just like precedence, associativity resolves the order of operationswhen two operators of the same precedence compete for three operands:</p><blockquote><pre class="code">4 ** 3 ** 2 # 4 ** (3 ** 2), or 4 ** 9 (right associative)72 / 12 / 3 # (72 / 12) / 3, or 6/3, or 2 (left associative)36 / 6 * 3 # (36/6)*3, or 18</pre></blockquote><p>In the first case, the <tt class="literal">**</tt> operator has rightassociativity, so the parentheses are implied on the right.Comparatively, the <tt class="literal">*</tt> and <tt class="literal">/</tt>operators have left associativity, yielding a set of impliedparentheses on the left.</p><p>So should you just memorize the precedence chart? No! Nobody actuallydoes that. Instead, just use parentheses when you don'tremember the order of operations, or when you're too busy tolook in the chart. After all, if you can't remember it withoutthe parentheses, your <a name="INDEX-218" />maintenance programmer is going to havethe same trouble. So be nice to your maintenanceprogrammer.<a name="INDEX-219" /> <a name="INDEX-220" /> <a name="INDEX-221" /> <a name="INDEX-222" /> </p></div><a name="lperl3-CHP-2-SECT-6.3" /><div class="sect2"><h3 class="sect2">2.6.3. Comparison Operators</h3><p>For comparing <a name="INDEX-223" /><a name="INDEX-224" />numbers, Perl has the logical<a name="INDEX-225" /><a name="INDEX-226" />comparisonoperators that remind you of algebra: <tt class="literal">< <= == >=> !=</tt>. Each of these returns a <em class="emphasis">true</em>or <em class="emphasis">false</em> value. We'll find out more aboutthose return values in the next section. Some of these may bedifferent than you'd use in other languages. For example,<tt class="literal">==</tt> is used for equality, not a single<tt class="literal">=</tt> sign, because that's used for anotherpurpose in Perl. And <tt class="literal">!=</tt> is used for inequalitytesting, because <tt class="literal"><></tt> is used for anotherpurpose in Perl. And you'll need <tt class="literal">>=</tt> andnot <tt class="literal">=></tt> for "greater than or equalto", because the latter is used for another purpose in Perl. Infact, nearly every sequence of punctuation is used for something inPerl.</p><p>For comparing strings, Perl has an equivalent set of<a name="INDEX-227" /><a name="INDEX-228" />string comparison operators which look likefunny little words: <tt class="literal">lt</tt> <tt class="literal">le</tt><tt class="literal">eq</tt> <tt class="literal">ge</tt> <tt class="literal">gt</tt><tt class="literal">ne</tt>. These compare two strings character bycharacter to see whether they're the same, or whether one comesfirst in standard string sorting order. (In ASCII, the capitalletters come before the lowercase letters, so beware.)</p><p>The comparison operators (for both numbers and strings) are given in<a href="ch02_06.htm#lperl3-CHP-2-TABLE-3">Table 2-3</a>.</p><a name="lperl3-CHP-2-TABLE-3" /><h4 class="objtitle">Table 2-3. Numeric and string comparison operators </h4><table border="1"><tr><th><p>Comparison</p></th><th><p>Numeric</p></th><th><p>String</p></th></tr><tr><td><p>Equal</p></td><td><p><tt class="literal">==</tt></p></td><td><p><tt class="literal">eq</tt></p></td></tr><tr><td><p>Not equal</p></td><td><p><tt class="literal">!=</tt></p></td><td><p><tt class="literal">ne</tt></p></td></tr><tr><td><p>Less than</p></td><td><p><tt class="literal"><</tt></p></td><td><p><tt class="literal">lt</tt></p></td></tr><tr><td><p>Greater than</p></td><td><p><tt class="literal">></tt></p></td><td><p><tt class="literal">gt</tt></p></td></tr><tr><td><p>Less than or equal to</p></td><td><p><tt class="literal"><=</tt></p></td><td><p><tt class="literal">le</tt></p></td></tr><tr><td><p>Greater than or equal to</p></td><td><p><tt class="literal">>=</tt></p></td><td><p><tt class="literal">ge</tt></p></td></tr></table><p><p>Here are some example expressions using these comparison operators:</p><blockquote><pre class="code">35 != 30 + 5 # false35 == 35.0 # true'35' eq '35.0' # false (comparing as strings)'fred' lt 'barney' # false'fred' lt 'free' # true'fred' eq "fred" # true'fred' eq 'Fred' # false' ' gt '' # true</pre></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch02_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.5. Scalar Variables</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">2.7. The if Control Structure</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><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 + -