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

📄 ch03_07.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Multiplicative Operators (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="Multiplicative Operators"><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_06.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_08.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.7. Multiplicative Operators</h2><p><a name="INDEX-843"></a><a name="INDEX-844"></a><a name="INDEX-845"></a><a name="INDEX-846"></a>Perl provides the C-like operators <tt class="literal">*</tt> (multiply), <tt class="literal">/</tt> (divide), and<tt class="literal">%</tt> (modulo). The <tt class="literal">*</tt> and <tt class="literal">/</tt> work exactly as you would expect,multiplying or dividing their two operands.  Division is done infloating point, unless you've used the <tt class="literal">integer</tt> pragmatic module.<a name="INDEX-847"></a><a name="INDEX-848"></a></p><p>The <tt class="literal">%</tt> operator converts its operands to integers before finding theremainder according to integer division.  (However, it does thisinteger division in floating point if necessary, so your operands canbe up to 15 digits long on most 32-bit machines.)  Assume that your twooperands are called <tt class="literal">$a</tt> and <tt class="literal">$b</tt>.  If <tt class="literal">$b</tt> is positive, then theresult of <tt class="literal">$a % $b</tt> is <tt class="literal">$a</tt> minus the largest multiple of <tt class="literal">$b</tt> thatis not greater than <tt class="literal">$a</tt> (which means the result will always be in therange <tt class="literal">0 .. $b-1</tt>).  If <tt class="literal">$b</tt> is negative, then the result of <tt class="literal">$a % $b</tt> is <tt class="literal">$a</tt> minus the smallest multiple of <tt class="literal">$b</tt> that is not less than<tt class="literal">$a</tt> (which means the result will be in the range <tt class="literal">$b+1 .. 0</tt>).</p><p>When <tt class="literal">use integer</tt> is in scope, <tt class="literal">%</tt> gives you direct access to themodulus operator as implemented by your C compiler.  This operator isnot well defined for negative operands, but will execute faster.</p><p><a name="INDEX-849"></a><a name="INDEX-850"></a><a name="INDEX-851"></a><a name="INDEX-852"></a>Binary <tt class="literal">x</tt> is the repetition operator.  Actually, it's two operators.In scalar context, it returns a concatenated string consisting of theleft operand repeated the number of times specified by the rightoperand.  (For backward compatibility, it also does this in listcontext if the left argument is not in parentheses.)<blockquote><pre class="programlisting">print '-' x 80;                             # print row of dashesprint "\t" x ($tab/8), ' ' x ($tab%8);      # tab over</pre></blockquote>In list context, if the left operand is a list in parentheses, the <tt class="literal">x</tt>works as a list replicator rather than a string replicator.  This isuseful for initializing all the elements of an array of indeterminatelength to the same value:<blockquote><pre class="programlisting">@ones = (1) x 80;           # a list of 80 1's@ones = (5) x @ones;        # set all elements to 5</pre></blockquote><a name="INDEX-853"></a><a name="INDEX-854"></a><a name="INDEX-855"></a>Similarly, you can also use <tt class="literal">x</tt> to initialize array and hash slices:<blockquote><pre class="programlisting">@keys = qw(perls before swine);@hash{@keys} = ("") x @keys;</pre></blockquote>If this mystifies you, note that<tt class="literal">@keys</tt> is being used both as a list on the left sideof the assignment and as a scalar value (returning the array length)on the right side of the assignment.  The previous example has thesame effect on <tt class="literal">%hash</tt> as:<blockquote><pre class="programlisting">$hash{perls}  = "";$hash{before} = "";$hash{swine}  = "";</pre></blockquote></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_06.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_08.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.6. Binding 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.8. Additive Operators</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 + -