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

📄 ch4.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML><HEAD><TITLE>Chapter 4 -- Operators</TITLE><META></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910"><H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;4</FONT></H1><H1><FONT SIZE=6 COLOR=#FF0000>Operators</FONT></H1><HR><P><CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER><UL><LI><A HREF="#OperatorTypes">Operator Types</A><LI><A HREF="#TheBinaryArithmeticOperators">The Binary Arithmetic Operators</A><UL><LI><A HREF="#ExampleTheExponentiationOperator">Example: The Exponentiation Operator</A><LI><A HREF="#ExampleTheModulusOperator">Example: The Modulus Operator</A></UL><LI><A HREF="#TheUnaryArithmeticOperators">The Unary Arithmetic Operators</A><UL><LI><A HREF="#ExampleThePreiNCrementOperator">Example: The Pre-iNCrement Operator</A><LI><A HREF="#ExampleThePredecrementOperator">Example: The Pre-decrement Operator</A><LI><A HREF="#ExampleThePostiNCrementOperator">Example: The Post-iNCrement Operator</A></UL><LI><A HREF="#TheLogicalOperators">The Logical Operators</A><UL><LI><A HREF="#ExampleThequotANDquotOperatorampamp">Example: The &quot;AND&quot; Operator (&amp;&amp;)</A><LI><A HREF="#ExampleThequotORquotOperator">Example: The &quot;OR&quot; Operator (||)</A></UL><LI><A HREF="#ExampleThequotNOTquotOperator">Example: The &quot;NOT&quot; Operator (!)</A><LI><A HREF="#TheBitwiseOperators">The Bitwise Operators</A><UL><LI><A HREF="#ExampleUsingtheampandOperators">Example: Using the &amp;, |, and ^ Operators</A><LI><A HREF="#ExampleUsingthegtgtandltltOperators">Example: Using the &gt;&gt; and &lt;&lt; Operators</A></UL><LI><A HREF="#TheNumericRelationalOperators">The Numeric Relational Operators</A><UL><LI><A HREF="#ExampleUsingtheltgtOperator">Example: Using the &lt;=&gt; Operator</A></UL><LI><A HREF="#TheStringRelationalOperators">The String Relational Operators</A><UL><LI><A HREF="#ExampleUsingthecmpOperator">Example: Using the cmp Operator</A></UL><LI><A HREF="#TheTernaryOperator">The Ternary Operator</A><UL><LI><A HREF="#ExampleUsingtheTernaryOperatortoAssignValues">Example: Using the Ternary Operator to Assign Values</A></UL><LI><A HREF="#TheRangeOperator">The Range Operator (..)</A><UL><LI><A HREF="#ExampleUsingtheRangeOperator">Example: Using the Range Operator</A></UL><LI><A HREF="#TheStringOperatorsandx">The String Operators (. and x)</A><UL><LI><A HREF="#ExampleUsingtheCoNCatenationOperator">Example: Using the CoNCatenation Operator</A><LI><A HREF="#ExampleUsingtheRepetitionOperator">Example: Using the Repetition Operator</A></UL><LI><A HREF="#TheAssignmentOperators">The Assignment Operators</A><UL><LI><A HREF="#ExampleAssignmentUsingArraySlices">Example: Assignment Using Array Slices</A><LI><A HREF="#ExampleAssigninganArraytoScalarVariables">Example: Assigning an Array to Scalar       Variables</A></UL><LI><A HREF="#OrderofPrecedeNCe">Order of PrecedeNCe</A><UL><LI><A HREF="#ExampleOrderofPrecedeNCe">Example: Order of PrecedeNCe</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>The <I>operators </I>in a computer language tell the computerwhat actions to perform. Perl has more operators than most languages.You've already seen some operators-like the equals or assignmentoperator(=)-in this book. As you read about the other operators,you'll undoubtedly realize that you are familiar with some ofthem. Trust your intuition; the definitions that you already knowwill probably still be true.<P>Operators are instructions you give to the computer so that itcan perform some task or operation. All operators cause actionsto be performed on <I>operands</I>. An operand can be anythingthat you perform an operation on. In practical terms, any particularoperand will be a literal, a variable, or an expression. You'vealready been introduced to literals and variables. A good workingdefinition of expression is some combination of operators andoperands that are evaluated as a unit. <A HREF="ch6.htm" >Chapter 6</A> &quot;Statements,&quot;has more information about expressions.<P>Operands are also <I>recursive</I> in nature. In Perl, the expression3 + 5-two operands and a plus operator-can be considered as oneoperand with a value of 8. For instaNCe, (3 + 5) - 12 is an expressionthat consists of two operands, the second of which is subtractedfrom the first. The first operand is (3 + 5) and the second operandis 12.<P>This chapter will discuss most of the operators available to youin Perl . You'll find out about many operator types and how todetermine their order of precedeNCe. And, of course, you'll seemany examples.<P>PrecedeNCe is very important in every computer language and Perlis no exception. The <I>order of precedeNCe</I> indicates whichoperator should be evaluated first.<P>I like to think about operators in the same way I would give instructionsto the driver of a car. I might say &quot;turn left&quot; or &quot;turnright.&quot; These commands could be considered directional operatorsin the same way that + and - say &quot;add this&quot; or &quot;subtractthis.&quot; If I yell &quot;stop&quot; while the car is moving,on the other hand, it should supersede the other commands. Thismeans that &quot;stop&quot; has precedeNCe over &quot;turn left&quot;and &quot;turn right.&quot; The &quot;Order of PrecedeNCe&quot;section later in this chapter will discuss precedeNCe in moredetail.<H2><A NAME="OperatorTypes"><FONT SIZE=5 COLOR=#FF0000>Operator Types</FONT></A></H2><P>Perl supports many types of operators. Table 4.1 shows all ofthe operator types in the Perl language. This chapter discussesthe more commonly used types in detail. You can learn about anytype not discussed in this chapter by looking in the chapter refereNCedin that type's description in Table 4.1.<BR><P><CENTER><B>Table 4.1&nbsp;&nbsp;The Perl Operator Types</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=145><I>Operator Types</I></TD><TD WIDTH=445><I>Description</I></TD></TR><TR><TD WIDTH=145>Arithmetic</TD><TD WIDTH=445>These operators mirror those you learned in grade school. Addition, Subtraction, and Multiplication are the bread and butter of most mathematical statements.</TD></TR><TR><TD WIDTH=145>Assignment</TD><TD WIDTH=445>These operators are used to assign a value to a variable. Algebra uses assignment operators. For example, in the statement X = 6, the equal sign is the assignment operator.</TD></TR><TR><TD WIDTH=145>Binding</TD><TD WIDTH=445>These operators are used during string comparisons and are ex-plained in <A HREF="ch10.htm" >Chapter 10</A>, &quot;Regular Expressions.&quot;</TD></TR><TR><TD WIDTH=145>Bitwise</TD><TD WIDTH=445>These operators affect the individual bits that make up a value. For example, the value 3 is also 11 in binary notation or ((1 <FONT FACE="Symbol">&#165;</FONT> 2) + 1). Each character in binary notation represents a <I>bit,</I> which is the smallest piece of a computer's memory that you can modify.</TD></TR><TR><TD WIDTH=145>Comma</TD><TD WIDTH=445>The comma operator has two fuNCtions. It serves to separate array or list elements (see <A HREF="ch2.htm" >Chapter 2</A> &quot;Numeric and String Literals&quot;) and it serves to separate expressions (see <A HREF="ch6.htm" >Chapter 6</A> &quot;Statements&quot;).</TD></TR><TR><TD WIDTH=145>File Test</TD><TD WIDTH=445>These operators are used to test for various conditions associated with files. You can test for file existeNCe, file type, and file access rights among other things. See <A HREF="ch9.htm" >Chapter 9</A> &quot;Using Files,&quot; for more information.</TD></TR><TR><TD WIDTH=145>List</TD><TD WIDTH=445>List operators are funny things in Perl. They resemble fuNCtion calls in other languages. List operators are discussed in <A HREF="ch5.htm" >Chapter 5</A> &quot;FuNCtions.&quot;</TD></TR><TR><TD WIDTH=145>Logical</TD><TD WIDTH=445>These operators implement Boolean or true/false logic. In the senteNCe &quot;If John has a fever AND John has clogged sinuses OR an earache AND John is NOT over 60 years old, then John has a cold,&quot; the AND, OR, and NOT are acting as logical operators. The low precedeNCe logical operators will be discussed separately in <A HREF="ch13.htm" >Chapter 13</A>, &quot;Handling Errors and Signals.&quot; </TD></TR><TR><TD WIDTH=145>Numeric Relational</TD><TD WIDTH=445>These operators allow you to test the Relationalrelationship of one numeric variable to another. For example, is 5 GREATER THAN 12?</TD></TR><TR><TD WIDTH=145>Postfix</TD><TD WIDTH=445>A member of this group of opera-tors-(), [], {}- appears at the end of the affected objects. You've already seen them used in <A HREF="ch3.htm" >Chapter 3</A> &quot;Vari-ables&quot; for arrays and associative arrays. The parentheses operators are also used for list operators as discussed in <A HREF="ch5.htm" >Chapter 5</A> &quot;FuNCtions.&quot;</TD></TR><TR><TD WIDTH=145>Range</TD><TD WIDTH=445>The range operator is used to create a range of elements in arrays. It can also be used in a scalar context.</TD></TR><TR><TD WIDTH=145>RefereNCe</TD><TD WIDTH=445>The refereNCe operators are used to manipulate variables. For more information, see <A HREF="ch8.htm" >Chapter 8</A> &quot;Refer-eNCes.&quot;</TD></TR><TR><TD WIDTH=145>String</TD><TD WIDTH=445>The string coNCatenation operator is used to join two strings together. The string repetition operator is used to repeat a string.</TD></TR><TR><TD WIDTH=145>String Relational</TD><TD WIDTH=445>These operators allow you to test the relationship of one string variable to another. For example, is &quot;abc&quot; GREATER THAN &quot;ABC&quot;?</TD></TR><TR><TD WIDTH=145>Ternary</TD><TD WIDTH=445>The ternary operator is used to choose between two choices based on a given condition. For instaNCe: If the park is within one mile, John can walk; otherwise, he must drive.</TD></TR></TABLE></CENTER><P><H2><A NAME="TheBinaryArithmeticOperators"><FONT SIZE=5 COLOR=#FF0000>The Binary Arithmetic Operators</FONT></A></H2><P>There are six <I>binary arithmetic operators</I>: addition, subtraction,multiplication, exponentiation, division, and modulus. While youmay be unfamiliar with the modulus operator, the rest act exactlyas you would expect them to. Table 4.2 lists the arithmetic operatorsthat act on two operands-the binary arithmetic operators. In otherwords, the addition (+) operator can be used to add two numberstogether like this: 4 + 5. The other binary operators act in asimilar fashion.<BR><P><CENTER><B>Table 4.2&nbsp;&nbsp;The Binary Arithmetic Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=151><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=132><I>Description</I></TD></TR><TR><TD WIDTH=151><CENTER><TT>op1 + op2</TT></CENTER></TD><TD WIDTH=132>Addition</TD></TR><TR><TD WIDTH=151><CENTER><TT>op1 - op2</TT></CENTER></TD><TD WIDTH=132>Subtraction</TD></TR><TR><TD WIDTH=151><CENTER><TT>op1 * op2</TT></CENTER></TD><TD WIDTH=132>Multiplication</TD></TR><TR><TD WIDTH=151><CENTER><TT>op1 ** op2</TT></CENTER></TD><TD WIDTH=132>Exponentiation</TD></TR><TR><TD WIDTH=151><CENTER><TT>op1 / op2</TT></CENTER></TD><TD WIDTH=132>Division</TD></TR><TR><TD WIDTH=151><CENTER><TT>op1 % op2</TT></CENTER></TD><TD WIDTH=132>Modulus</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleTheExponentiationOperator">Example: The Exponentiation Operator</A></H3><P>The <I>exponentiation</I> operator is used to raise a number toa power. For instaNCe, 2 <FONT FACE="Symbol">**</FONT>4 is equivalentto 2 <FONT FACE="Symbol">* </FONT> 2 <FONT FACE="Symbol">*</FONT>2 <FONT FACE="Symbol">*</FONT> 2, which equals 16. You'll occasionallysee a refereNCe to when exponentiation discussion turns to howefficient a given algorithm is, but I've never needed it for myeveryday programming tasks. In any case, here's a quick look athow it works.<P>This example shows how to raise the number 4 to the 3<FONT SIZE=1>rd</FONT>power, which is equivalent to 4 <FONT FACE="Symbol">*</FONT> 4<FONT FACE="Symbol">*</FONT> 4 or 64.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign </I><TT><I>$firstVar</I></TT><I><FONT FACE="MCPdigital-BI"></FONT>the value of 4.<BR>Raise 4 to the 3<I><FONT SIZE=1>rd</FONT> power using the exponentiationoperator and assign the new value to </I></I><TT><I>$secondVar</I></TT><I><FONT FACE="MCPdigital-BI">.<BR></FONT>Print </I><TT><I>$secondVar</I></TT><I><FONT FACE="MCPdigital-BI">.</FONT></I></BLOCKQUOTE><BLOCKQUOTE><PRE>$firstVar = 4;$secondVar = $firstVar ** 3;print(&quot;$secondVar\n&quot;);</PRE></BLOCKQUOTE><P>This program produces the following output:<BLOCKQUOTE><PRE>64</PRE></BLOCKQUOTE><H3><A NAME="ExampleTheModulusOperator">Example: The Modulus Operator</A></H3><P>The <I>modulus </I>operator is used to find the remainder of thedivision between two integer operands. For instaNCe, 10 % 7 equals3 because 10 / 7 equals 1 with 3 left over.<P>I've found the modulus operator to be useful when my programsneed to run down a list and do something every few items. Thisexample shows you how to do something every 10 items.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start a loop that begins with<FONT FACE="MCPdigital-BI"> </FONT></I><TT><I>$index</I></TT><I><FONT FACE="MCPdigital-BI"></FONT>equal to zero.<BR>If the value of </I><TT><I>$index</I></TT><I><FONT FACE="MCPdigital-BI">% </FONT></I><TT><I>10</I></TT><I>is equal to zero then the </I><TT><I>print</I></TT><I>statement will be executed.<BR>Print the value of </I><TT><I>$index</I></TT><I>followed by space.<BR>The program will iNCrease the value of </I><TT><I>$index</I></TT><I>by one and then loop back to the start of the </I><TT><I>if</I></TT><I>statement.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 4.1&nbsp;&nbsp;O4LST01.PL-How to Display a MessageEvery Ten Items<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>for ($index = 0; $index &lt;= 100; $index++) {    if ($index % 10 == 0) {        print(&quot;$index &quot;);    }}</PRE></BLOCKQUOTE><HR><P>When this program is run, the output should look like the following:<BLOCKQUOTE><PRE>0 10 20 30 40 50 60 70 80 90 100</PRE></BLOCKQUOTE><P>Notice that every tenth item is printed. By changing the valueon the right side of the modulus operator, you can affect howmany items are processed before the message is printed. Changingthe value to 15 means that a message will be printed every 15items. <A HREF="ch7.htm" >Chapter 7</A> &quot;Control Statements,&quot; describes the<TT>if</TT> and <TT>for</TT>statement in detail.<H2><A NAME="TheUnaryArithmeticOperators"><FONT SIZE=5 COLOR=#FF0000>The Unary Arithmetic Operators</FONT></A></H2><P>The unary arithmetic operators act on a single operand. They areused to change the sign of a value, to iNCrement a value, or todecrement a value. <I>INCrementing</I> a value means to add oneto its value. <I>Decrementing</I> a value means to subtract onefrom its value. Table 4.3 lists Perl's unary operators.<BR><P><CENTER><B>Table 4.3&nbsp;&nbsp;The Unary Arithmetic Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=175><CENTER><I>Operator</I></CENTER></TD><TD WIDTH=217><I>Description</I></TD></TR><TR><TD COLSPAN=2 WIDTH=392><B>Changing the sign of op1</B></TD></TR><TR><TD WIDTH=175><CENTER>+<TT>op1</TT></CENTER></TD><TD WIDTH=217>Positive operand</TD></TR><TR><TD WIDTH=175><CENTER>-<TT>op1</TT></CENTER></TD><TD WIDTH=217>Negative operand</TD></TR><TR><TD COLSPAN=2 WIDTH=392><B>Changing the value of op1 before usage</B></TD></TR><TR><TD WIDTH=175><CENTER>++<TT>op1</TT></CENTER></TD><TD WIDTH=217>Pre-iNCrement operand by one</TD></TR><TR><TD WIDTH=175><CENTER>--<TT>op1</TT></CENTER></TD><TD WIDTH=217>Pre-decrement operand by one</TD></TR><TR><TD COLSPAN=2 WIDTH=392><B>Changing the value of op1 after usage</B></TD></TR>

⌨️ 快捷键说明

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